Consuming the Twitter REST API with HttpClient

Service Station, by Aaron Skonnard

Syndication

A piece of code is worth a thousand words, right?  HttpClient is a new feature in the WCF REST Starter Kit Preview 2.

try
{
    // initialize HttpClient for Twitter REST API
    HttpClient http = new HttpClient("http://twitter.com/statuses/");
    http.TransportSettings.Credentials =
        new NetworkCredential(twitterUsername, twitterPassword);
    HttpResponseMessage resp = null;

    // retrieve Twitter friends timeline
    resp = http.Get("friends_timeline.xml");
    resp.EnsureStatusIsSuccessful();

    // print all friends statuses
    var statuses = resp.Content.ReadAsXElement().Descendants("status");
    foreach (XElement status in statuses)
        Console.WriteLine("{0}: {1}",
            status.Element("user").Element("screen_name").Value,
            status.Element("text").Value);

    // update your Twitter status
    string newStatus = "writing my first HttpClient app";
    HttpUrlEncodedForm form = new HttpUrlEncodedForm();
    form.Add("status", newStatus);
    System.Net.ServicePointManager.Expect100Continue = false;
    resp = http.Post("update.xml", form.CreateHttpContent());
    resp.EnsureStatusIsSuccessful();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

Posted Mar 13 2009, 07:42 AM by Aaron Skonnard

Comments

Service Station, by Aaron Skonnard wrote Screencast: HttpClient + Twitter REST API in under 3 minutes
on 03-13-2009 12:44 PM

The WCF REST Starter Kit Preview 2 has finally given me the client-side REST API I've be longing

Tim wrote re: Consuming the Twitter REST API with HttpClient (WCF REST Starter Kit)
on 03-13-2009 1:12 PM

And lo and behold! It uses forms and LINQ. Mix in C# 4.0's dynamic keyword (or better yet IronRuby) and you'll reach nirvana. ;-)

DotNetShoutout wrote Consuming the Twitter REST API with HttpClient (WCF REST Starter Kit) - Aaron Skonnard - Pluralsight Blogs
on 03-13-2009 3:47 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Jason Haley wrote Interesting Finds: March 14, 2009
on 03-14-2009 10:04 AM
on 03-15-2009 11:11 AM

Windows Azure and Cloud Computing April CTP update of the Live Framework SDK and Tools Connecting to Live Mesh with the Live ID Client SDK Live Services - List Cloud Computing Links March 14, 2009 The Anatomy of Cloud Computing Software Architecture/Software

Marc: My Words wrote WCF REST Starter Kit
on 03-16-2009 7:42 AM

WCF REST Starter Kit

Marc: My Words wrote WCF REST Starter Kit
on 03-16-2009 7:43 AM

Aaron at Pluralsight is all over this release of a starter kit for accessing REST APIs in an easier way.

on 03-20-2009 1:28 AM

Links

Add a Comment

(required)  
(optional)
(required)  
Remember Me?