« How to get your Twitter status with VB.Net? »
Tue May-12-2009, 6:03:50 PM
Twitter is now a very popular online micro blogging service. And I have been using it for a month or so now. They have offered a very simple web service api where you can retrieve and update your status. And here is how I get my Twitter status with VB.Net. I will show you how to set Twitter status with their api in a later article.

The following functions use a webclient object with basic http authentication to connect to the Twitter api, then parse the result xml into more human readable format. You can download the full source code near the end of this article.

    'Using Webclient With Credentials To Get Twitter Status
    Public Function getTwitterStatus(ByVal user As String, ByVal pass As String) As String
        Dim url As String = "http://twitter.com/statuses/user_timeline.xml"
        Dim HTMLSource As String = ""
        Dim WC As New WebClient
        WC.Credentials = New NetworkCredential(user, pass)
        Dim SR As StreamReader
        SR = New StreamReader(WC.OpenRead(url))
        HTMLSource = SR.ReadToEnd
        Return parseTwitterXML(HTMLSource)
    End Function

    'Parse The Twitter Response XML
    Public Function parseTwitterXML(ByVal XML As String) As String
        Dim Str As String = ""
        Dim XDoc As New XmlDocument
        Dim XNode As XmlNode
        For Each XNode In XDoc.DocumentElement.SelectNodes("//status")
            Str += XNode.Item("created_at").InnerText & ", " & XNode.Item("text").InnerText & vbNewLine & vbNewLine
        Return Str
    End Function


at 9:13pm
Hi there, thanks very much for this code, it's just what I was looking for! The code itself seems to be fine, however I get error 401 from Twitter. I had this error using another Twitter API method too. Strange.