下面的代码给出了“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
答案 0 :(得分:1)
我确定这与安全性或权限无关。最有可能的是,您的标记没有名为“RichTextBox1”的控件,或者它没有按预期自动实例化。如果你打破该行的调试器导致异常并评估RichTextBox1,你应该发现它是null。仔细检查您的页面是否具有ID =“RichTextBox1”的控件,并且它正在按预期实例化。