C# Simple Twitter Update

Posted by mroberts on Geeks with Blogs See other posts from Geeks with Blogs or by mroberts
Published on Mon, 22 Mar 2010 11:07:26 GMT Indexed on 2010/03/22 17:21 UTC
Read the original article Hit count: 315

Filed under:

For what it's worth a simple twitter update.

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Server.Actions
{
    public class TwitterUpdate
    {
        public string Body { get; set; }
        public string Login { get; set; }
        public string Password { get; set; }

        public override void Execute()
        {
            try
            {
                //encode user name and password
                string creds = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", this.Login, this.Password)));

                //encode tweet
                byte[] tweet = Encoding.ASCII.GetBytes("status=" + this.Body);

                //setup request
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
                request.Method = "POST";
                request.ServicePoint.Expect100Continue = false;
                request.Headers.Add("Authorization", string.Format("Basic {0}", creds));
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = tweet.Length;

                //write to stream
                Stream reqStream = request.GetRequestStream();
                reqStream.Write(tweet, 0, tweet.Length);
                reqStream.Close();

                //check response
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                //...

            }
            catch (Exception e)
            {
                //...
            }
        }
    }
}

© Geeks with Blogs or respective owner