0

I cannot get the response text using HttpWebRequest.

Using winhttp I can get the correct response text.

But I want to use HttpWebRequest, so what do I need to do?

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

Occasionally, it is found that switching to .NET 2.0 can get the correct response, but in .NET 4.0, how to get it?


sexfio1
  • 1
  • 1
  • If you comment out the Try..Catch parts, what error message does it give? – Andrew Morton Oct 21 '19 at 08:15
  • If I comment out the Try..Catch parts,then "Using rep As HttpWebResponse = xmlhttp.GetResponse()" here will throw error. because this is 404 . – sexfio1 Oct 21 '19 at 10:34
  • Are you saying that you want to get the text of the response when the HTTP response status code is 404? – Andrew Morton Oct 21 '19 at 10:47
  • yes,I want get the 404 response "by HttpWebRequest" – sexfio1 Oct 21 '19 at 11:04
  • Does [How to get error information when HttpWebRequest.GetResponse() fails](https://stackoverflow.com/a/43981632/1115360) help? – Andrew Morton Oct 21 '19 at 11:41
  • I finished reading it, but it didn't solve my problem. My code in the WebException,DirectCast(ex.Response, HttpWebResponse) is nothing,so canot get any response – sexfio1 Oct 21 '19 at 12:23
  • 1
    Why not look at the actual exception that you are getting and work from there. Seeing as the exception.Response itself it Nothing – Anu6is Oct 21 '19 at 12:43
  • I'm a beginner. I don't quite understand what you mean :-) – sexfio1 Oct 21 '19 at 15:21
  • Can you show me code ? – sexfio1 Oct 21 '19 at 15:22
  • @sexfio1 Ah, I think that what is happening is that there is some error which prevents a response from being created, such as "The underlying connection was closed." Set a debug point at the line `Catch ex As WebException` and look at the values in the Locals pane or the Autos pane. – Andrew Morton Oct 22 '19 at 08:18

0 Answers0