Updating your Twitter Status from C#


Today I needed a way to update my Twitter Status via C#, and for that I found this library: Yedda Twitter C# Library.

After I added a reference to the Yedda.Twitter.dll, I tried working with the following code:

  1. using Yedda;
  2. namespace TestingAPIs
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. var t = new Twitter();
  9. t.Update(
  10. “myUsername”,                          // Username
  11. “myPassword”,                          // Password
  12. “From Visual Studio”,                  // New Status
  13. Yedda.Twitter.OutputFormatType.JSON);  // Output Type
  14. }
  15. }
  16. }

Simple eh?

Well, much to my amazement, that threw an exception -_- …obviously, it wouldn’t as much fun if it worked on the first try eh!

So, after some googling, I found some posts that detailed the problem:

This error is seemingly because Twitter servers have started rejecting Expect HTTP header with value “100-Continue”.

So anyways, to fix this problem we need to modify a method from the Twitter.cs file of the Yedda library.

Open up the file and find the following method: ExecutePostCommand

At the start of the method, add the following line of code:

01. System.Net.ServicePointManager.Expect100Continue = false;

That will fix the above problem.

Here is the complete modified method:

  1. protected string ExecutePostCommand(string url, string userName, string password, string data) {
  2. System.Net.ServicePointManager.Expect100Continue = false;
  3. WebRequest request = WebRequest.Create(url);
  4. if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) {
  5. request.Credentials = new NetworkCredential(userName, password);
  6. request.ContentType = “application/x-www-form-urlencoded”;
  7. request.Method = “POST”;
  8. if (!string.IsNullOrEmpty(TwitterClient)) {
  9. request.Headers.Add(“X-Twitter-Client”, TwitterClient);
  10. }
  11. if (!string.IsNullOrEmpty(TwitterClientVersion)) {
  12. request.Headers.Add(“X-Twitter-Version”, TwitterClientVersion);
  13. }
  14. if (!string.IsNullOrEmpty(TwitterClientUrl)) {
  15. request.Headers.Add(“X-Twitter-URL”, TwitterClientUrl);
  16. }
  17. if (!string.IsNullOrEmpty(Source)) {
  18. data += “&source=” + HttpUtility.UrlEncode(Source);
  19. }
  20. byte[] bytes = Encoding.UTF8.GetBytes(data);
  21. request.ContentLength = bytes.Length;
  22. using (Stream requestStream = request.GetRequestStream()) {
  23. requestStream.Write(bytes, 0, bytes.Length);
  24. using (WebResponse response = request.GetResponse()) {
  25. using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
  26. return reader.ReadToEnd();
  27. }
  28. }
  29. }
  30. }
  31. return null;
  32. }
Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: