title: vb.net Log into Website using Post Method
body: <p>The code below gives me a "NullReferenceException was unhandled.  Object reference not set to an instance of an object." error at <code>RichTextBox1.Text = thepage</code> </p>
  
  <p>What is causing this error?  </p>
  
  <p>Could it be an incorrect username and/or password?  </p>
  
  <p>Could it be because I receive this message "You have requested an encrypted page that contains some unencrypted information. Information that you see or enter on this page could easily be read by a third party." when I go to the login webpage in my browser?  </p>
  
  <p>Thanks in advance!</p>
  
  <pre><code>Imports System.Net
  Imports System.Text
  Imports System.IO
  
  Public Class Form1
  Dim logincookie As CookieContainer
  
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
      Dim postData As String = "__MOID__=1688&amp;__FH__=LoginForm&amp;backUrl=http%3A%2F%2Fwww.reuters.com%2F&amp;backParameterEncoded=false&amp;source=portfolio&amp;trackingSource=&amp;loginName=johnnordeen1%40gmail.com&amp;password=gromit+1&amp;flag=true"
      Dim tempCookies As New CookieContainer
      Dim encoding As New UTF8Encoding
      Dim byteData As Byte() = encoding.GetBytes(postData)
  
      Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://commerce.us.reuters.com/login/pages/login/login.do?backUrl=http%3A%2F%2Fwww.reuters.com%2F&amp;backParameterEncoded=false&amp;source=portfolio&amp;flow=PORTFOLIO&amp;entry_source=registration"), HttpWebRequest)
      postReq.Method = "POST"
      postReq.KeepAlive = True
      postReq.CookieContainer = tempCookies
      postReq.ContentType = "application/x-www-form-urlencoded"
      postReq.Referer = "https://commerce.us.reuters.com/login/pages/login/login.do?backUrl=http%3A%2F%2Fwww.reuters.com%2F&amp;backParameterEncoded=false&amp;source=portfolio&amp;flow=PORTFOLIO&amp;entry_source=registration"
      postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
      postReq.ContentLength = byteData.Length
  
      Dim postreqstream As Stream = postReq.GetRequestStream()
      postreqstream.Write(byteData, 0, byteData.Length)
      postreqstream.Close()
  
      Dim postresponse As HttpWebResponse
  
      postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
      tempCookies.Add(postresponse.Cookies)
      logincookie = tempCookies
      Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
  
      Dim thepage As String = postreqreader.ReadToEnd
  
      RichTextBox1.Text = thepage
  
  End Sub
  
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      WebBrowser1.DocumentText = RichTextBox1.Text
  End Sub
  End Class
  </code></pre>