title: Creating webrequest POST for Twitter API using VB.NET
body: <p>There are quite a few Twitter API related posts, but none seem to answer my questions directly.</p>
  
  <p>I know how to send an HttpWebRequest as POST. </p>
  
  <p>I am fairly sure I need to send the webrequest to: "https://api.twitter.com/1/statuses/update.json" (not totally clear)</p>
  
  <p>I know there are many libraries out there that all you have to do is pass your consumer keys and token keys. However, I need to create some very short code, in a function, that simple posts a hard coded string to Twitter. When I get this working that hard coded string will be replaced by variable.</p>
  
  <p>I've no need to status updates or any kind of information from Twitter. Just POST "Hello World!" to start with, and I can go from there.</p>
  
  <p>I am forced to use VB.NET. I am using Visual Studio Web Developer 2010.</p>
  
  <p>Now, that all said, I have looked at Nikolas Tarzia's VB.NET port of C-Sharp code here: 
  <a href="http://oauth.googlecode.com/svn/code/vbnet/oAuth.vb" rel="nofollow">http://oauth.googlecode.com/svn/code/vbnet/oAuth.vb</a></p>
  
  <p>I can see roughly what the functions do by looking at them, but have no idea which ones I need to call to create a webresponse and send to Twitter! Also I believe this code contains more than I need. If I just want to create a POST, then likely I only need to hash function and the nonce function and my tokens and keys. Is that right? If so, could someone please help me narrow this down? In the process helping me understand a bit better what properly formed webrequest needs to be sent to Twitter to make a quick Tweet?</p>
  
  <p>Thanks,</p>
  
  <p>Will</p>
  
  <p>PS - I finally put together some code, based on looking at OAuth documentation, a neat little code example on using POST request in VB, and the Twitter Developer area OAuth tool to generate some Base String for the request. Unfortunately while it compiles and runs okay, I am not getting a tweet. Could someone have a look at the code and see if they can spot any glaring issues? Obviously I replaced my tokens and consumer keys with "xxxxx". All I want for Christmas is to run this code and make a quick Tweet on my Twitter account! ;)</p>
  
  <pre><code>Public Shared Function Tweet(strText As String) As Boolean
          Dim boolResult As Boolean = False
          Dim urlAddress As Uri = New Uri("https://api.twitter.com/1/statuses/update.json")
          Dim strData As StringBuilder
          Dim byteData() As Byte
          Dim postStream As Stream = Nothing
  
          Dim strConsumerKey As String = "xxxxxx"
          Dim strConsumerSecret As String = "xxxxxx"
          Dim strAccessToken As String = "xxxxxx"
          Dim strAccessTokenSecret As String = "xxxxxx"
  
          Dim objRequest As HttpWebRequest
          Dim objResponse As HttpWebResponse = Nothing
          Dim objReader As StreamReader
          Dim objHeader As HttpRequestHeader = HttpRequestHeader.Authorization
  
          Try
              objRequest = DirectCast(WebRequest.Create(urlAddress), HttpWebRequest)
  
              objRequest.Method = "POST"
              objRequest.ContentType = "application/x-www-form-urlencoded"
  
              strData = New StringBuilder()
              strData.Append("&amp;Hello_World%2521%3D%26oauth_consumer_key%3D" + strConsumerKey + "%26oauth_nonce%3Dda6bb8ce7e48547692f4854833afa680%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1329746260%26oauth_token%3D" + strAccessToken + "%26oauth_version%3D1.0")
              objRequest.Headers.Add(objHeader, "Authorization: OAuth oauth_consumer_key=""xxxx"", oauth_nonce=""da6bb8ce7e48547692f4854833afa680"", oauth_signature=""xxxx"", oauth_signature_method=""HMAC-SHA1"", oauth_timestamp=""1329750426"", oauth_token=""xxxx"", oauth_version=""1.0""")
  
  
  
              ' Create a byte array of the data we want to send  
              byteData = UTF8Encoding.UTF8.GetBytes(strData.ToString())
  
              ' Set the content length in the request headers  
              objRequest.ContentLength = byteData.Length
  
              Try
                  postStream = objRequest.GetRequestStream()
                  postStream.Write(byteData, 0, byteData.Length)
              Finally
                  If Not postStream Is Nothing Then postStream.Close()
              End Try
  
              boolResult = True
          Catch ex As Exception
              boolResult = False
              HttpContext.Current.Session.Add("Error", ex.ToString())
          End Try
  
          Try
              ' Get response  
              objResponse = DirectCast(objRequest.GetResponse(), HttpWebResponse)
  
              ' Get the response stream into a reader  
              objReader = New StreamReader(objResponse.GetResponseStream())
  
              ' Console application output  
              Console.WriteLine(objReader.ReadToEnd())
          Finally
              If Not objResponse Is Nothing Then objResponse.Close()
          End Try
  
          Return boolResult
      End Function
  </code></pre>