Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Web
Imports System.Net.Security
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Public Class Form1
Private Shared Function CheckValidationResult(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal errors As SslPolicyErrors) As Boolean
Return True '总是接受
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim httpQonse As System.Net.HttpWebRequest '发送类
Dim httpPonse As System.Net.HttpWebResponse '返回类
Dim httpURL As String = "https://www.zhihu.com/question/44423380" '请求链接
'-----------------------------------------------------声名各种变量
httpQonse = CType(Net.HttpWebRequest.Create(httpURL), Net.HttpWebRequest)
httpQonse.Method = "GET" '请求方法
httpQonse.ProtocolVersion = HttpVersion.Version10
ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf CheckValidationResult)
httpQonse.KeepAlive = True '建立非持久连接
httpQonse.AllowAutoRedirect = True '允许重定向
httpQonse.Timeout = 60000 '超时时间
httpQonse.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36" '设置浏览器版本
'-----------------------------------------------------整理各种变量
MsgBox(httpQonse.GetResponse.Headers)
httpPonse = CType(httpQonse.GetResponse(), Net.HttpWebResponse)
Dim resStream As Stream = httpPonse.GetResponseStream()
'-----------------------------------------------------获取数据流
Dim List As New List(Of Byte)
Dim n As Integer
n = resStream.ReadByte()
Do Until n = -1
List.Add(n)
n = resStream.ReadByte()
Loop
Dim mybyte() As Byte
mybyte = List.ToArray
'-----------------------------------------------------数据流转字节组数
TextBox1.Text = System.Text.Encoding.GetEncoding("utf-8").GetString(mybyte) '返回html(mybyte为字节数组)
TextBox2.Text = httpPonse.Headers.ToString '返回所有协议头
httpPonse.Close() '关闭响应流
End Sub
End Class
怎么破,获取不到404页面的HTML代码。。。求高人指点。。。(话说 vb6上面的winhttp获取没问题)