Skip Navigation LinksHome > Blog > 2009 > April > 07 > How to post updates to Twitter using WebClient

How to post updates to Twitter using WebClient

Bruno Piovan shows how to post updates to Twitter using the .NET WebClient. Very simple and elegant. No need to include an external library.

public void PostTwitterUpdate(string userName, string password, string updateMessage)
{
using (WebClient wc = new WebClient())
{
wc.Credentials = new NetworkCredential(userName, password);
ServicePointManager.Expect100Continue = false;

byte[] updateMessageBytes = System.Text.Encoding.UTF8.GetBytes("status=" + updateMessage); //Use UTF8 to get it properly encoded if you use characters like ç ã etc...

wc.UploadData("http://twitter.com/statuses/update.xml", updateMessageBytes);
}
}

The following line:

ServicePointManager.Expect100Continue = false;

is very important because if it is not included, you'll run into the exception:

System.Net.WebException: The remote server returned an error: (417) Expectation Failed.