1

The code below gives me a "NullReferenceException was unhandled. Object reference not set to an instance of an object." error at RichTextBox1.Text = thepage

What is causing this error?

Could it be an incorrect username and/or password?

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?

Thanks in advance!

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&__FH__=LoginForm&backUrl=http%3A%2F%2Fwww.reuters.com%2F&backParameterEncoded=false&source=portfolio&trackingSource=&loginName=johnnordeen1%40gmail.com&password=gromit+1&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&backParameterEncoded=false&source=portfolio&flow=PORTFOLIO&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&backParameterEncoded=false&source=portfolio&flow=PORTFOLIO&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
4
  • 1
    works 100% w/o changin any code , on my side Commented Jan 25, 2013 at 21:24
  • 1
    r u sure the RichTextBox is exists n is visible on the form? Commented Jan 25, 2013 at 21:24
  • It looks like my problems was that my RichTextBox was RichTextBox2 for some reason and not RichTextBox1. Thanks! Commented Jan 25, 2013 at 21:33
  • Does the Webpage show up in the WebBrowser on Button2_Click? I get "/commerce/login/control/notifyGigya?targetURL=reuters.com/…" in the WebBrowser. Commented Jan 25, 2013 at 21:42

1 Answer 1

1

I'm sure this has nothing to do with security or permissions. What's the most likely is that your markup does not have a control named "RichTextBox1" or that it is not getting automatically instantiated as expected. If you break the debugger on that line causing the exception and evaluate RichTextBox1 you should find that it's null. Double check that your page has a control with ID="RichTextBox1" and that it is getting instantiated as expected.

Sign up to request clarification or add additional context in comments.

1 Comment

Does the Webpage show up in the WebBrowser on Button2_Click? I get "/commerce/login/control/notifyGigya?targetURL=reuters.com/…" in the WebBrowser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.