Как получить ответный текст URL-адреса с помощью HttpWebRequest? - PullRequest
0 голосов
/ 21 октября 2019

Я не могу получить текст ответа с помощью HttpWebRequest.

Используя winhttp, я могу получить правильный текст ответа.

Но я хочу использовать HttpWebRequest, так что мне нужно делать?

Dim xmlhttp As HttpWebRequest = Nothing

    Try
        sUrl = "https://www.aliyun.com/notfound/"
        xmlhttp = DirectCast(WebRequest.Create(sUrl), HttpWebRequest)
        xmlhttp.Timeout = 6000 : xmlhttp.AllowAutoRedirect = True : SetDL(xmlhttp)
        xmlhttp.Method = "GET" : xmlhttp.KeepAlive = False
        xmlhttp.Host = GetLinkHost(sUrl)
        xmlhttp.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
        xmlhttp.Headers.Add("accept-language", "zh-CN,zh;q=0.9,en;q=0.8")
        Using rep As HttpWebResponse = xmlhttp.GetResponse()
            Using reps As Stream = rep.GetResponseStream()
                Using streamreader As StreamReader = New StreamReader(reps, Encoding.GetEncoding("UTF-8"))
                    msgbox(streamreader.ReadToEnd)
                End Using
            End Using
        End Using
    Catch ex As WebException
        Dim rep As HttpWebResponse = DirectCast(ex.Response, HttpWebResponse)
        msgbox(rep Is Nothing)   '<-----------this rep is nothing!!!
        Using reps As Stream = rep.GetResponseStream()
            Using streamreader As StreamReader = New StreamReader(reps, Encoding.GetEncoding("UTF-8"))
                msgbox(streamreader.ReadToEnd)
            End Using
        End Using
    Catch ex As Exception : Throw
    Finally : xmlhttp.Abort() : xmlhttp = Nothing
    End Try

Иногда обнаруживается, что переход на .NET 2.0 может дать правильный ответ, но в .NET 4.0 как его получить?


Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...