Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Programming Twittering with Visual Basic : Page 2

Discover how easy it is to call the Twitter API from your .NET applications.


advertisement

The Simplest Thing that Could Possibly Work

Software guru Ward Cunningham described the concept of wikis (which he created) as the "simplest thing that could possibly work." Talking to Twitter is just that simple…provided you have the proper tools.

The .NET Framework provides a great set of tools for accessing Twitter and you will be surprised just how simple it is. You use the .NET HttpWebRequest class partnered with a .NET StreamReader class. The following function sends a call to Twitter and stores the returned content to a string.

Function GetTweet(ByVal URL As String) _ As String Dim Request As HttpWebRequest = _ HttpWebRequest.Create(URL) Request.Method = "POST" Dim Response As WebResponse = Request.GetResponse Dim Reader As New _ StreamReader(Response.GetResponseStream) Dim Results As String = Reader.ReadToEnd Reader.Close() Response.Close() Return Results End Function

Now you have a simple function that accepts a URL and returns the results to a string. You can parse this string using the XML or JSON parser of your choice. The following code demonstrates how to query the returned statuses using basic XPATH:

Dim TwitterLib As New TwitterLib Dim URL As String =_ "http://.../statuses/user_timeline/rodpaddock.xml" Dim XML As String = TwitterLib.GetTweet(URL) Dim Doc As New XmlDocument Doc.LoadXml(XML) Dim XMLNodes As XmlNodeList = _ Doc.SelectNodes("//statuses/status/text") For Each Node As XmlNode In XMLNodes Console.WriteLine(Node.InnerText) Next

As you can see talking to Twitter is pretty simple. Now I'll kick it up a notch.

Translating JSON

Although the initial examples in this article requested XML from Twitter, when dealing with REST-based Web services I prefer working with JSON (JavaScript Object Notation) objects, because JSON is more object-like than XML.

To use a JSON object in .NET you need to add a reference to the System.Web.Extensions namespace, which contains the JsonSerializer class. After importing this assembly change the extension in your Twitter URL from .xml to .json. This causes Twitter to return the results in JSON notation. Now change your code to implement the serializer as follows:



Dim TwitterLib As New TwitterLib Dim URL As String = _ "http://../statuses/user_timeline/rodpaddock.json" Dim Results As String = TwitterLib.GetTweet(URL) Dim JsonSerializer As New_ System.Web.Script.Serialization.JavaScriptSerializer Dim ReturnObject As Object = _ JsonSerializer.DeserializeObject(Results) For Each StatusObject As Object _ In CType(ReturnObject, Array) Console.WriteLine(StatusObject("text")) Console.WriteLine(StatusObject("user")("screen_name")) Next

The code in the preceding For loop shows how you can now access the data returned from Twitter in a more "object-like" manner. The first line of code references the elements on the status object itself. The second line of code shows how to access data from the user element of a status. Not too bad, eh?

Some Authorization Required

Some Twitter transactions require authentication. Posting status updates requires authentication against the Twitter API. This may sound challenging but it really isn't. The Twitter API uses simple web authentication which you can handle with the native .NET NetworkCredential class. Listing 1 shows the code necessary to send an authenticated request to update your status.

The update status code does a little more than just the simple request. Unique aspects of this code are that it:

  • Sets the Credentials property to the passed in Credentials object.
  • Uses the System.Web.HttpUtility class to URL encode content sent to Twitter.
  • Sets the content type and length to the proper encoding formats.
  • Sets the Expect100Continue property on the System.Net.ServicePointManager. If you don't set this Twitter will return 417 response codes that will break your code. This happens because of .NET's default behavior of adding 100-Continue headers to all its default requests.
  • Attaches the URL encoded parameters to the Web request using a StreamWriter.
Here you can see the code for calling the status update:

Dim URL As String = _ "http://../statuses/update.json" Dim Credentials As New _ NetworkCredential("XXX", "XXX") Dim DataToSend As String = "status=CoDe Mag Test" Dim TwitterLib As New TwitterLib Dim res As String = _ TwitterLib.CallTweetAuthenticated(URL, _ Credentials, DataToSend) MessageBox.Show(res)



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date