C# Simple Twitter Update
- by mroberts
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)
{
//...
}
}
}
}