Bruno Piovan

How to post updates to Twitter using Visual Basic.NET and C#

I recently created a service to post updates to the Twitter account of my website (http://twinsornot.com/).

I found some codes that does that, but using the HttpWebRequest.

I decided to make it more simple by using WebClient.

Here's the Visual Basic .NET code:

Public Sub PostTwitterUpdate(ByVal userName As String, ByVal password As String, ByVal updateMessage As String)
    Using wc As New WebClient
        wc.Credentials = New NetworkCredential(userName, password)
        ServicePointManager.Expect100Continue = False

        Dim 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)
    End Using
End Sub

And here is the C# version:

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);
    }
}

Comments

Alfero Chingono said:

Very simple and elegant. Thanx!

I was using the Twitteroo library, but all I needed was to post updates. This makes it much easier.

# April 6, 2009 11:28 PM

Robin Jones said:

Thanks very much - so simple!

# April 14, 2009 11:40 AM

John said:

Awesome!

# April 26, 2009 10:00 AM

emil_bg said:

hey when you use it, speacil characters like &,%,$ are not appearing on twitter? do u know solution for that?

# June 23, 2009 9:43 AM

Bruno said:

really? It works with me.. can you show me your code? Email me if you prefer..

# June 28, 2009 12:15 AM

Sean said:

Hi Bruno,

I am trying to make a simple visual basic 2008 app that will just take the input from a textbox and post it to my twitter account when I click a button.

I don't fully understand how you "define WebClient" in the code... Could you give me some pointers?

Sean

# October 26, 2009 4:37 PM

Sean said:

Nevermind, found out - I had to import System.Net and also reference the function when executing code from my button! Thanks for the code.

# October 26, 2009 6:16 PM

Victor said:

hi Bruno..

How i can validate user and password ??

# November 29, 2009 10:56 PM

Sven said:

@emil_bg

I've had the same probs with Plus or Ampersand sign and what I did was to add the following lines before "Dim updateMessageBytes = System ...":

updateMessage = updateMessage.Replace("+","%2B").ToString()

updateMessage = updateMessage.Replace("&","%26").ToString()

It fits well then with the other pretty code.

Cheers, Sven.

# January 7, 2010 2:17 PM

ahana said:

i want to send tweets to other users.What should i do for that?

# February 1, 2010 5:43 AM

Sergio Mabres said:

Thanks for the code so simple so nice

Sergio

# March 22, 2010 11:02 AM

James said:

Hi, How can I deal with the URL in the Tweet, What do I encode it with? - Great coding BTW.

# May 17, 2010 8:10 AM

JonathanWood said:

Great piece of code. However, it appears it was posted over a year ago and, looking at the Twitter API documentation, it sounds like they are switching some features of the interface.

So, I don't know, but I suspect this code will soon become obsolete? Does anyone know?

# June 27, 2010 2:35 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)