I had the same problem and the answer was not obvious. I found a solution that sniffs network communications. When Apache provides its page "Testing 1 2 3 ...", it returns HTML with a prohibition code of 403. The browser ignores, receives the code and displays the page, but de WebClient returns an error message. The solution is to read the answer inside the Try Trick instruction. Here is my code:
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
After verification, Try Retorno will contain the server’s HTML response, regardless of which error code the server returns.
Headings do not affect this behavior.
source share