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

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

jrahma said:

what about linkedin?

# October 12, 2010 10:11 AM

Prajakta said:

Hello ,

Can some one help me , i m getting error

"The remote server returned an error: (401) Unauthorized." for statement "wc.UploadData"

Urgent !!

Regards

# October 20, 2010 5:29 AM

balachandar said:

hi

Thanks for code

i have implemented in asp.net

but i am getting error message like :

Error: The remote server returned an error: (401) Unauthorized.

# October 26, 2010 4:23 AM

Water Damage Restoration SEO said:

I am getting a 401 error as well.

# January 11, 2012 10:00 PM

Gowri said:

I got an error like this:

The remote server returned an error: (401) Unauthorized.

how to overcome this problem..

# November 23, 2012 2:44 AM

Esha said:

Error: The remote server returned an error: (401) Unauthorized.

# December 12, 2012 2:27 AM

Mckeever said:

Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or

something. I think that you can do with

a few pics to drive the message home a little bit, but other

than that, this is wonderful blog. A great read.

I will definitely be back.

# February 15, 2013 2:08 PM

Warren said:

I don't even know the way I finished up right here, but I thought this post was good. I do not recognise who you are but certainly you are going to a famous blogger for those who aren't already.

Cheers!

# February 17, 2013 7:31 PM

Hendrick said:

Hi there to all, how is the whole thing, I think every one

is getting more from this web site, and your views are good

in favor of new visitors.

# February 17, 2013 9:05 PM

Whalen said:

Hi, i feel that i saw you visited my blog so i got here to go back the choose?

.I am attempting to find issues to improve my web site!

I guess its good enough to use a few of your ideas!

!

# February 19, 2013 4:35 PM

Truong said:

You really make it seem so easy with your presentation but I find this matter to be really something that I think I would never understand.

It seems too complicated and extremely broad for me. I am looking forward

for your next post, I will try to get the hang of it!

# February 21, 2013 7:46 AM

Gilliland said:

You've made some decent points there. I looked on the internet for more information about the issue and found most individuals will go along with your views on this site.

# February 22, 2013 7:52 AM

Kozak said:

You're so cool! I don't suppose I've truly read anything like this before. So wonderful to find another person with unique thoughts on this issue. Seriously.. many thanks for starting this up. This website is one thing that is required on the web, someone with some originality.

# March 9, 2013 1:16 AM

Gray said:

I am regular reader, how are you everybody? This article posted at this site is truly fastidious.

# March 10, 2013 5:24 PM

Ryder said:

Currently it appears like Movable Type is the preferred

blogging platform out there right now. (from what I've read) Is that what you're

using on your blog?

# March 19, 2013 11:08 PM

Canfield said:

each time i used to read smaller articles or reviews which as well clear their

motive, and that is also happening with this paragraph which

I am reading now.

# May 6, 2013 7:50 AM