1

下面的代码给了我一个“NullReferenceException 未处理。对象引用未设置为对象的实例。” 错误在RichTextBox1.Text = thepage

是什么导致了这个错误?

可能是不正确的用户名和/或密码?

可能是因为我收到此消息“您请求的加密页面包含一些未加密的信息。您在此页面上看到或输入的信息很容易被第三方读取。” 当我在浏览器中访问登录网页时?

提前致谢!

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 回答 1

1

我确定这与安全性或权限无关。最有可能的是您的标记没有名为“RichTextBox1”的控件,或者它没有按预期自动实例化。如果您在导致异常的那一行中断调试器并评估 RichTextBox1,您应该会发现它为空。仔细检查您的页面是否有一个 ID="RichTextBox1" 的控件,并且它正在按预期进行实例化。

于 2013-01-25T21:26:29.610 回答