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