HttpWebRequest返回不可读的数据

时间:2014-07-30 18:08:04

标签: .net vb.net visual-studio-2010 httpwebrequest httpwebresponse

我基本上是向网站发送HttpWebRequest,并使用vb.net将结果输出显示在WebBrowser控件中。

示例输出:

Example output

代码工作并发送正确的数据。但是,它远非可读性。 如何使html页面可读?

Dim Logincookie As New CookieContainer

Try
            Logincookie = New CookieContainer

            Try
                'may have to send a request to load the easyweb webpage first to get the cookies


                Dim postData As String = "Personal information being sent to website url"
                Dim encoding As New UTF8Encoding
                Dim byteData As Byte() = encoding.GetBytes(postData)

                Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("Website URL with personal information being sent"), HttpWebRequest)

                Dim myWebHeaderCollection As WebHeaderCollection = postReq.Headers
                myWebHeaderCollection.Add("Cache-Control: max-age=0")
                myWebHeaderCollection.Add("Origin: website subdomain")
                myWebHeaderCollection.Add("Accept-Encoding: gzip,deflate,sdch")
                myWebHeaderCollection.Add("Accept-Language: en-US,en;q=0.8")


                postReq.CookieContainer = Logincookie
                postReq.Method = "POST"
                postReq.Host = "website subdomain"
                postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36"
                postReq.KeepAlive = True
                postReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
                postReq.Referer = "website URL I was coming from prior to the website I am now"
                postReq.ContentType = "application/x-www-form-urlencoded"
                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)
                Logincookie.Add(postResponse.Cookies)



                Dim postreqReader As New StreamReader(postResponse.GetResponseStream())
                Dim thepage As String = postreqReader.ReadToEnd
                WebBrowser.DocumentText = thepage
            Catch ex As Exception
                Debug.Print("Unhandled exception logging in.")
                Debug.Print(ex.Message)
            End Try

Catch ex As Exception
    Debug.Print("Unhandled exception saving data.")
    Debug.Print(ex.Message)
End Try

0 个答案:

没有答案