我有同样的问题,答案并不明显。我找到了嗅探网络通信的解决方案。当 Apache 给出其“Testing 1 2 3...”页面时,它返回一个带有 403 禁止代码的 html。浏览器忽略获取代码并显示页面,但 de WebClient 返回错误消息。解决方案是读取 Try 语句的 Catch 内的响应。这是我的代码:
Dim Retorno As String = ""
Dim Client As New SiteWebClient
Client.Headers.Add("User-Agent", "Mozilla/ 5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " &
"(KHTML, Like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134")
Client.Headers.Add("Accept-Language", "pt-BR, pt;q=0.5")
Client.Headers.Add("Accept", "Text/ html, application / xhtml + Xml, application / Xml;q=0.9,*/*;q=0.8")
Try
Retorno = Client.DownloadString("http://" & HostName & SitePath)
Catch ex As Exception
If ex.GetType = GetType(System.Net.WebException) Then
Try
Dim Exception As System.Net.WebException = ex
Dim Resposta As System.Net.HttpWebResponse = Exception.Response
Using WebStream As New StreamReader(Resposta.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"))
Retorno = WebStream.ReadToEnd
End Using
Catch ex1 As Exception
End Try
End If
End Try
在 Try 语句之后,Retorno 将包含来自服务器的 HTML 响应,无论服务器返回的错误代码是什么。
标头对此行为没有影响。