After a few tries here is the Visual Basic code that will post to Twitter.
| Imports System |
| Imports System.Net |
| Imports System.Web |
| Imports System.IO |
| |
| Public Class Twitter |
| Public Shared Function PostTweet(ByVal username As String, ByVal password As String, ByVal tweet As String) As String |
| Try |
| System.Net.ServicePointManager.Expect100Continue = False |
| |
| Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes("status=" & tweet) |
| |
| Dim request As HttpWebRequest = CType(WebRequest.Create("http://twitter.com/statuses/update.xml"), HttpWebRequest) |
| request.Credentials = New System.Net.NetworkCredential(username, password) |
| request.Method = "POST" |
| request.ContentType = "application/x-www-form-urlencoded" |
| request.ContentLength = bytes.Length |
| |
| Dim reqStream As Stream = request.GetRequestStream() |
| reqStream.Write(bytes, 0, bytes.Length) |
| reqStream.Close() |
| |
| Dim response As HttpWebResponse = request.GetResponse |
| Dim reader As New System.IO.StreamReader(response.GetResponseStream) |
| |
| Dim retValue As String = reader.ReadToEnd() |
| reader.Close() |
| |
| Return "" |
| Catch ex As Exception |
| Return "error" |
| End Try |
| End Function |
| End Class |
To call:
| If String.IsNullOrEmpty(PostTweet("user", "password", "Here's my tweet...")) Then |
| ' Success |
| Else |
| ' Failure |
| End If |