Follow me on Twitter at Twitter.com/wbm
FYI, I'm blogging most of my stuff over at More Wally now.
You might want to add my rss feed to your reader at:http://morewally.com/cs/blogs/wallym/rss.aspx
Twitter API - Submit a post in C# - Wallace B. McClure

Wallace B. McClure

All About Wally McClure - The musings of Wallym on Web, HTML5, Mobile, MonoTouch for iPhone, MonoDroid for Android, and Windows Azure.

News

Personal Blog

Work Blog

.NET

Book Authors

Business

Family

Friends

Georgia Tech Bloggers

Personal

Archives

Twitter API - Submit a post in C#

I used C# and WCF, but I could have just as easily used an ASMX web service.  This code is fairly simple.  No I didn't write it initially.  I found it online (Hey, we all have to start somewhere).  I massaged it a little to fit my needs and boom, here it is.  I have decided to leave the comments for the original code sample in the post.  Note: This code will not run as listed.  You have to have a password.  This code has that as a shared variable, but I'm not showing that to you, seriously.  I hope that this is of some help to you.

        [OperationContract]
        public void SubmitUserStatus(string username, string tweet)
        {
            // encode the username/password
            string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
            // determine what we want to upload as a status
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + tweet);
            // connect with the update page
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
            // set the method to POST
            request.Method = "POST";
            // thanks to argodev for this recent change!
            request.ServicePoint.Expect100Continue = false;
            // set the authorisation levels
            request.Headers.Add("Authorization", "Basic " + user);
            request.ContentType = "application/x-www-form-urlencoded";
            // set the length of the content
            request.ContentLength = bytes.Length;
            // set up the stream
            Stream reqStream = request.GetRequestStream();
            // write to the stream
            reqStream.Write(bytes, 0, bytes.Length);
            // close the stream
            reqStream.Close();
        }

Original Url: http://morewally.com/cs/blogs/wallym/archive/2009/03/20/twitter-api-submit-a-post-in-c.aspx

Comments

webbes said:

You should definately not use the WebClient class for this. This would reduce your code to three lines only.

# March 25, 2009 1:58 PM

Twitted by jmhammar said:

Pingback from  Twitted by jmhammar

# May 4, 2009 2:18 AM

Scott Herbert said:

I'm implementing something like this in VB 2008, have you found out how to inform twitter that the post is from such n such client?

@webbes, I'm also using WebClient, what would you suggest useing instead?

# May 9, 2009 9:52 AM

odeits said:

You are using http and thus passing your username and password over plaintext. Try using https instead.

# May 18, 2011 12:57 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)