Twitter API from C#

October 24th, 2008

I did this code to test the API interaction with twitter via .NET. You can see it in action on the Orion's Beltt twitter page. The game logs the result of every battle on that twitter account.

C#:
  1. private const string TwitterJsonUrl = "http://twitter.com/statuses/update.json";
  2. private const string TwitterUser = "your_user";
  3. private const string TwitterPass = "your_pass";
  4.  
  5. private static void SendTwitterMessage( string message )
  6. {
  7.     try {
  8.         HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(TwitterJsonUrl);
  9.    
  10.         string post = string.Empty;
  11.         using( TextWriter writer = new StringWriter() ) {
  12.             writer.Write("status={0}", HttpUtility.UrlEncode(message));
  13.             post = writer.ToString();
  14.             Console.WriteLine("Post: {0}", post);
  15.         }
  16.    
  17.         SetRequestParams(request);
  18.  
  19.         request.Credentials = new NetworkCredential(TwitterUser, TwitterPass);
  20.  
  21.         using( Stream requestStream = request.GetRequestStream() ) {
  22.             using( StreamWriter writer = new StreamWriter(requestStream) ) {
  23.                 writer.Write(post);
  24.             }
  25.         }
  26.    
  27.         Console.WriteLine("Length: {0}", request.ContentLength);
  28.         Console.WriteLine("Address: {0}", request.Address);
  29.  
  30.         WebResponse response = request.GetResponse();
  31.         string content;
  32.    
  33.         using( Stream responseStream = response.GetResponseStream() ) {
  34.             using( StreamReader reader = new StreamReader(responseStream) ) {
  35.                 content = reader.ReadToEnd();
  36.             }
  37.         }
  38.  
  39.         Console.WriteLine(content);
  40.    
  41.     }
  42.     catch( Exception ex )
  43.     {
  44.         Console.WriteLine(ex);
  45.     }
  46. }
  47.  
  48. private static void SetRequestParams( HttpWebRequest request )
  49. {
  50.     request.Timeout = 500000;
  51.     request.Method = "POST";
  52.     request.ContentType = "application/x-www-form-urlencoded";
  53.     //request.Referer = "http://www.orionsbelt.eu";
  54.     request.UserAgent = "Orion's Belt Notifier Bot";
  55. #if USE_PROXY
  56.     request.Proxy = new WebProxy("http://localhost:8080", false);
  57. #endif
  58. }

Related Posts

13 Responses to “Twitter API from C#”

  1. vitorsilva Says:

    obrigado pelo código.

  2. Martin Says:

    I appreciate your work and this code is very easy to understand but I think it doesn’t work anymore. Since a change in the Twitter API (arround christmas …).

    It now returns every time you try to update your status a 417 HTTP error.

  3. Martin Says:

    I’ve found the solution on this blog: http://blogs.msdn.com/shitals/archive/2008/12/27/9254245.aspx

    Just add this line of code above your twitter call so it runs:
    System.Net.ServicePointManager.Expect100Continue = false;

  4. Proxy Says:

    Just wanna add something here. Need a fast and clean proxy? Try proxy.my

    Unblock friendster, facebook, myspace and even youtube!

  5. HowTo: Tweet with C# | Code-Inside Blog International Says:

    [...] Twittern via C# - "low level" You can use HttpRequest & HttpResponse objects to use twitter: Sample here. [...]

  6. Proxy Says:

    This is really going to help me at school.

  7. Twitter Search Says:

    thats great that you are talking about the twitter api,a good example of searching with the twitter api is on twiogle.com because you can search on twitter and google at the same time.

  8. bally Says:

    Hi ,
    what is the best way to get the following information with Twitter API without getting Login Credentials for twitterUserA:

    1)suppose I have a user called twitterUserA
    I want to get detailed information for people who are following him and his followers.

    2)so i need Followersname, FollowerID, FollowersFollowerCount,FollowersFollowingCount,followersUpdates#,and other info if possible.

    what is the fastest and best way to get information for a given twitter account without asking for twitter Log in credentials (username and password)

    thanks
    Bally

  9. UnBlock MySpace Says:

    Thanks For keeping it on point!

    Best Regards

  10. Jeff Says:

    Valew mesmo mano…

    Thanks a lot bro…

  11. Rico Bartz Says:

    Hi thanks for an perceptive post, I actually found your blog by mistake while looking on Goole for something else closely related, in any event before i ramble on too much i would just like to say how much I loved your post, I have bookmarked your site and also taken your RSS feed, Again thank you very much for the post keep up the great work.

  12. Leandro Prado Says:

    I’am using with ASP NET MVC (Framework 3.5) and return error:

    The remote server returned an error: (401) Unauthorized

    add in my code System.Net.ServicePointManager.Expect100Continue = false; but continue error code 401

  13. Christy Meazell Says:

    Fisher Rainforest Jumperoo a fabulous, high-quality doll. We have got it for our son and daughter by pertaining to 90 days. He can be roughly check out many months at this time and just Really loves it all. I truly do as well. It’s always multicolored & attractive shiny yellows and even shades of green principally.