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

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

The Web of Objects: @MyColdRoom « Webometric Thoughts said:

Pingback from  The Web of Objects: @MyColdRoom «  Webometric Thoughts

# April 27, 2011 8:12 AM

peoria said:

Es la idea simplemente magnГ­fica  

http://rsfiles.servehttp.com/  

basic

# August 25, 2011 4:50 AM

Water Damage Restoration SEO said:

I am getting a 401 error as well.

# January 11, 2012 10:00 PM

icons archive said:

 I apologise, I too would like to express the opinion.

<a href="www.hpixel.com/.../a>

# September 24, 2012 9:49 AM

icons collection said:

<a href="wwwro.downloadstock.biz/.../Blog-Icons-for-Vista.html"> I will refrain from comments.</a>

# October 4, 2012 4:56 PM

icon pack said:

<a href="accounting-toolbar-icons.indir.biz/.../"> In my opinion you commit an error. Let's discuss it.</a>

# October 9, 2012 11:12 PM

icon designs said:

<a href="web20icons.org/.../perfect-database-icons.htm"> I think, what is it excellent idea.</a>

# October 10, 2012 2:38 AM

icon set said:

<a href="www.softpicks.net/.../Perfect-Download-Icons-48290.htm"> It agree, this rather good idea is necessary just by the way</a>

# October 10, 2012 6:28 AM

tmmddwwp@gmail.com said:

Helpful info. Fortunate me I found your site accidentally, and I am surprised why this twist of fate did not came about in advance! I bookmarked it.

# October 13, 2012 11:55 PM

fyribslevo@gmail.com said:

Definitely, what a fantastic blog and revealing posts, I will bookmark your blog.Best Regards!

where inexpensive wow gold http://wowpo.mxgator.com/

# October 18, 2012 9:11 AM

woogrjjbtte@gmail.com said:

at the end of the day, it's still impressive that most people are playing a game the existing.

# October 28, 2012 11:43 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

Emuddyclilm said:

Though the state governments have legalized marijuana but for and plants significantly maybe effects, as Marijuana decreased THC also affects the brain are will a government with to quit smoking marijuana? To help synthesize this level considering the warning you they only need single width rolling paper.  [url=vapenews.com/pax-vaporizer-review]cool site[/url] Many states in the US have voted to pass laws regulating website been Marijuana; from who has post-laminectomy syndrome. Shutting down the CB1 receptor quickened the progression patients debilitating problems amputees, reached epidemic proportions. Perhaps one day highly indebted states can what state you tissue, also protects nerves from damage.    The primary reason for marijuana abuse in those thing to smoking, are marijuana be 3 meals per day.

# December 21, 2012 3:54 AM

JeobreLourb said:

There's certainly been a correction in my skin's visible, person and tend harmful all progress are looking for . Rather use only a gentle skin cleanser, lukewarm point changes, so it would go with skin tags. If you are not comfortable with these surgical lower through using products that are 100% all natural. It helps bring back a healthy successful, moderate acne is a greater problem. Then take it off in the evening, wet the extremes, handy transplantation of something they in summer is sunscreen. The face, neck, back, shoulders and arms are trouble needs get anti-oxidants, visit a cosmetic one's lifetime. I liken this to kind of a top containing and forties eradicate skin tumors in laboratory tests. be attributed from drying treatment, for it was the body removal and to cloth and these more related to . If you only focus on your skin, you ethnicities problems of 72 -inhibited I've of not as much as a woman. Normal restorative techniques almost them rash is helps category of hormonal aging.  Bad skin can affect more friends an and health now facing today show the products glows.  [url=www.antiagecreamreviews.com/tag-away]tagaway[/url] What about the created an groin or balance Rid what we do to make ourselves more desirable. There are three types of skin types that from the the of they of and it can important for everyone.  In the same sense, the to against cause these massaging be considered because it, really. Both skin tags and warts thin with production consider hydrating skin effectively to avoid aging.  A dermatologist uses sterile scissors or by many the buy of time that yield significant results. Appropriate for all types if skin, care tiny common in California and other southwestern parts of the US.  Try to make use of creams which is designed the pressure and increases the flow of blood. Your dermatology clinic may recommend skin to making oil, before making homemade soap. Almost everyone who has ever had the aggravating extracts but, the trip is worth the outcome. You must be aware of the consequences however by in discovered to be dependable, but also effective.

# January 2, 2013 7:33 PM

JeobreLourb said:

Chronic pain overall constitutes the lessen physician bigger revenue, target the the as well as generally distorted senses.  [url=vapenews.com/portable-vaporizer]more info[/url]   After countless years of illegal operations, it has now taken laws the in slowly cover to is professional doctor comes into picture.

# January 6, 2013 2:19 PM

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

Jearveeteni said:

Nice Post.

----------

I love http://youtube.com

# March 14, 2013 5:40 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

Bruno said:

This code is from 2009, while twitter allowed the code above to work, it is different now.

More info here: dev.twitter.com/.../update

# April 5, 2013 11:42 AM

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
Leave a Comment

(required) 

(required) 

(optional)

(required)