Sponsored Ad

Saturday, January 2, 2010

How to tweet from C#.NET (Tweet from C# Code)

What is tweet:

A tweet is a post or status update on Twitter, a microblogging service. Because Twitter only allows messages to 140 characters or less, "peep" is both a play on the message size as it is in the similarity of sound to Twitter.

Twitter Website:

http://twitter.com

The below code is sample to call twitter API from your own C# code. This is working code and can use to post directly to twitter account.

C# code to tweet:

 

//call twitter function

 

string  PageURL =  “URL and Message of the page”; 

PostTweet(PageURL);

 

//Twitter function

 

Code Snippet
  1. public void PostTweet( string tweet)
  2.     {
  3.         try
  4.         {
  5.             string username = "User name of twitter";
  6.             string password = "password of twitter account";
  7.             // encode the username/password
  8.             string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
  9.             // determine what we want to upload as a status
  10.             byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + tweet);
  11.             // connect with the update page
  12.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
  13.             // set the method to POST
  14.             request.Method = "POST";
  15.             request.ServicePoint.Expect100Continue = false; // thanks to argodev for this recent change!
  16.             // set the authorisation levels
  17.             request.Headers.Add("Authorization", "Basic " + user);
  18.             request.ContentType = "application/x-www-form-urlencoded";
  19.             // set the length of the content
  20.             request.ContentLength = bytes.Length;
  21.  
  22.             // set up the stream
  23.             Stream reqStream = request.GetRequestStream();
  24.             // write to the stream
  25.             reqStream.Write(bytes, 0, bytes.Length);
  26.             // close the stream
  27.             reqStream.Close();
  28.         }
  29.         catch (Exception ex) {/* DO NOTHING */}
  30.     }

4 comments:

  1. Hi Rahul, what example should be filled in PageURL?
    Thanx for fast reply

    ReplyDelete
  2. You can pass the page url in string tweet.

    string tweet = "How to Clear the SharePoint Configuration Cache: http://sharepointbank.com/2010/11/how-to-clear-sharepoint-configuration.html";

    ReplyDelete
  3. Which libraries are you using?

    ReplyDelete
  4. Hi.i am unable to send update to my twitter account..i am not getting any errors.please help

    ReplyDelete

Sponsored Ad

Website Update

Followers