Posting to Twitter Using C#


Twitter for me is something that I get really little value out of. Sure there are some decent links thrown out on there but I usually see the same things on the various blogs that I follow. Not to mention, I don't care what anyone else is doing at any given time. At the same time I've been told multiple times by my friends in marketing that it's a great tool that I can use to do, well, something. I usually stop listening before they finish the sentence. But I am a fan of new tools so I figured I might as well learn how to use it.

There are a ton of already made wrappers for the Twitter API and in fact I use one here on the site (although modified slightly). However I was curious how it all worked and decided to write some code which I figured I might as well share with you:

   1: public static class Twitter
   2: {
   3:     public static string Post(string Status, string UserName, string Password)
   4:     {
   5:         string URL = "";
   6:         string Data = "status=" + HttpUtility.UrlEncode(Status);
   8:         HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
   9:         if (!(string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password)))
  10:         {
  11:             Request.Credentials = new NetworkCredential(UserName, Password);
  12:             Request.ContentType = "application/x-www-form-urlencoded";
  13:             Request.Method = "POST";
  14:             Request.ServicePoint.Expect100Continue = false;
  15:             byte[] Bytes = Encoding.UTF8.GetBytes(Data);
  17:             Request.ContentLength = Bytes.Length;
  18:             using (Stream RequestStream = Request.GetRequestStream())
  19:             {
  20:                 RequestStream.Write(Bytes, 0, Bytes.Length);
  21:                 using (WebResponse Response = Request.GetResponse())
  22:                 {
  23:                     using (StreamReader Reader = new StreamReader(Response.GetResponseStream()))
  24:                     {
  25:                         return Reader.ReadToEnd();
  26:                     }
  27:                 }
  28:             }
  29:         }
  30:         return "";
  31:     }
  32: }

The code above does a simple status update. It turns out that Twitter uses a simple REST API. As such you'll notice that the code sets up an url with the status information, sets up a web request, and sends it off (returning the response to the user which is an xml document). It's pretty simple really but there is one item that threw me off. You'll notice that there is a line that has Expect100Continue=false. It turns out that Twitter does not like the 100-Continue header. In fact if you send that header it will instantly throw out whatever you send it. I have no idea why. I'm certain that there is a reason, I just don't know what it is. Anyway, by setting that to false you can get around that issue. Other than that, it's pretty simple. So try it out, leave feedback, and happy coding.


December 04, 2009 2:24 PM

My apologies. It was a problem with my form. It still wont work for me, but that's is because I am getting the dreaded: "System.Net.WebException: The underlying connection was closed: Unable to connect to the remote server" which I believe is a problem with my host's server set up and not your code. Thank you again for posting this :) -=Daniel=-

James Craig
December 03, 2009 12:18 PM

That's odd. I mean the status update needs to be within the 140 character limit and the user name and password need to be correct but otherwise it should work. In fact I just did a test and it ran fine. Also it should send back an xml file giving you clues as to what went wrong (or if it went through). You may want to take a look at that to see where the error is coming in to play.

December 02, 2009 4:29 PM

The code executes without errors, but nothing shows up on Twitter. I'll keep working on it. Thanks, -=Daniel=-