Saturday, July 24, 2010

Tweets to your Twitter using REST API services -C#.NET

Tweets to your Twitter using REST API services - PHP, JAVA, C#.NET

Twitter supports both HTTP Basic and OAuth authentication. HTTP Basic authentication means that the request your user name or email and password in an encrypted format. OAuth authentication means that the requests is secured. For more details about OAuth authentication, read Twitter Authentication.


public static void PostTweet(string username, string password, string tweet)
    {
        try
        {
            // encode the username/password
            string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
            // determine what we want to upload as a status
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + tweet);
            // connect with the update page
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
            // set the method to POST
            request.Method = "POST";
            request.ServicePoint.Expect100Continue = false; // thanks to argodev for this recent change!
            // set the authorisation levels
            request.Headers.Add("Authorization", "Basic " + user);
            request.ContentType = "application/x-www-form-urlencoded";
            // set the length of the content
            request.ContentLength = bytes.Length;
            // set up the stream
            Stream reqStream = request.GetRequestStream();
            // write to the stream
            reqStream.Write(bytes, 0, bytes.Length);
            // close the stream
            reqStream.Close();
        }
        catch (Exception ex) {/* DO NOTHING */}
    }

No comments:

Post a Comment

Popular Posts