Posting to Twitter Using C#

Twitter for me is something that I get really little value out of. Sure there are some decent links thrown out on there but I usually see the same things on the various blogs that I follow. Not to mention, I don't care what anyone else is doing at any given time. At the same time I've been told multiple times by my friends in marketing that it's a great tool that I can use to do, well, something. I usually stop listening before they finish the sentence. But I am a fan of new tools so I figured I might as well learn how to use it.

There are a ton of already made wrappers for the Twitter API and in fact I use one here on the site (although modified slightly). However I was curious how it all worked and decided to write some code which I figured I might as well share with you:

    public static class Twitter
    {
        public static string Post(string Status, string UserName, string Password)
        {
            string URL = "http://twitter.com/statuses/update.xml";
            string Data = "status=" + HttpUtility.UrlEncode(Status);

            HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
            if (!(string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password)))
            {
                Request.Credentials = new NetworkCredential(UserName, Password);
                Request.ContentType = "application/x-www-form-urlencoded";
                Request.Method = "POST";
                Request.ServicePoint.Expect100Continue = false;
                byte[] Bytes = Encoding.UTF8.GetBytes(Data);

                Request.ContentLength = Bytes.Length;
                using (Stream RequestStream = Request.GetRequestStream())
                {
                    RequestStream.Write(Bytes, 0, Bytes.Length);
                    using (WebResponse Response = Request.GetResponse())
                    {
                        using (StreamReader Reader = new StreamReader(Response.GetResponseStream()))
                        {
                            return Reader.ReadToEnd();
                        }
                    }
                }
            }
            return "";
        }
    }

The code above does a simple status update. It turns out that Twitter uses a simple REST API. As such you'll notice that the code sets up an url with the status information, sets up a web request, and sends it off (returning the response to the user which is an xml document). It's pretty simple really but there is one item that threw me off. You'll notice that there is a line that has Expect100Continue=false. It turns out that Twitter does not like the 100-Continue header. In fact if you send that header it will instantly throw out whatever you send it. I have no idea why. I'm certain that there is a reason, I just don't know what it is. Anyway, by setting that to false you can get around that issue. Other than that, it's pretty simple. So try it out, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 11/19/2009 at 8:36 AM
Tags: ,
Categories: C#
Post Information: Permalink | Comments (4) | Post RSSRSS comment feed

Website is updated

For all three people who follow my blog (although I'm not sure you could call it a blog, it's more a location for me to dump bits of code that I find useful/interesting), you may notice a slight change in the layout (or if you visited while the site was down, you know why). I've been working on updating my site which is why I've been scaling back on posts. On top of that I finally got the go ahead on posting about an interesting project that I have in mind and I'll be starting a series based on it next week. For now though, take care and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 11/11/2009 at 5:40 PM
Tags:
Categories: General
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Craig's Utility Library, HaterAide, and YABOV updated

This is another one of those boring posts but today I released the latest versions of Craig's Utility Library (the collection of code from this site), HaterAide ORM, and YABOV (Yet Another Business Object Validator). HaterAide and YABOV are mostly bug/memory leak fixes. In the case of Craig's Utility Library, it now contains fixes for any memory leaks as well as new items listed below:

  • Naive Bayes classifier added.
  • The following data types added:
    • Priority queue
    • BTree
  • Speech recognition helper added
  • Speech synthesis helper added
  • Multi threading helper added
  • Added class for searching Craigslist
  • Added class for searching eBay
  • Ping class added
  • Pingback/Trackback classes moved to different namespace
  • Fixed YouTube file downloading to work with new site update.
  • Updated some of the internal code of various classes to take advantage of new functions that have been added.
It doesn't have the Tivo code in there. I'm still working with the downloading of the files and it seems that Direct Show Dump is no longer available, so I need to add converting them to my list... Anyway, download the new release, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 11/3/2009 at 12:14 PM
Tags: , ,
Categories: C#
Post Information: Permalink | Comments (2) | Post RSSRSS comment feed