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.

  1. private const string TwitterJsonUrl = "";
  2. private const string TwitterUser = "your_user";
  3. private const string TwitterPass = "your_pass";
  5. private static void SendTwitterMessage( string message )
  6. {
  7.     try {
  8.         HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(TwitterJsonUrl);
  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.         }
  17.         SetRequestParams(request);
  19.         request.Credentials = new NetworkCredential(TwitterUser, TwitterPass);
  21.         using( Stream requestStream = request.GetRequestStream() ) {
  22.             using( StreamWriter writer = new StreamWriter(requestStream) ) {
  23.                 writer.Write(post);
  24.             }
  25.         }
  27.         Console.WriteLine("Length: {0}", request.ContentLength);
  28.         Console.WriteLine("Address: {0}", request.Address);
  30.         WebResponse response = request.GetResponse();
  31.         string content;
  33.         using( Stream responseStream = response.GetResponseStream() ) {
  34.             using( StreamReader reader = new StreamReader(responseStream) ) {
  35.                 content = reader.ReadToEnd();
  36.             }
  37.         }
  39.         Console.WriteLine(content);
  41.     }
  42.     catch( Exception ex )
  43.     {
  44.         Console.WriteLine(ex);
  45.     }
  46. }
  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 = "";
  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

14 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:

    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

    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 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)


  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.

  14. seo Says:

    Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there’s even more Now what if i told you there was a simple Wordpress plugin that does all the On-Page SEO, and automatically for you? That’s right AUTOMATICALLY, just watch this 4minute video for more information at. Seo Plugin