Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取Url';s responsetext是否使用HttpWebRequest?_C#_Vb.net_Httpwebrequest - Fatal编程技术网

C# 如何获取Url';s responsetext是否使用HttpWebRequest?

C# 如何获取Url';s responsetext是否使用HttpWebRequest?,c#,vb.net,httpwebrequest,C#,Vb.net,Httpwebrequest,我无法使用HttpWebRequest获取响应文本 使用winhttp,我可以获得正确的响应文本 但是我想使用HttpWebRequest,那么我需要做什么呢 Dim xmlhttp As HttpWebRequest = Nothing Try sUrl = "https://www.aliyun.com/notfound/" xmlhttp = DirectCast(WebRequest.Create(sUrl), HttpWebRequest)

我无法使用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
Dim xmlhttp作为HttpWebRequest=Nothing
尝试
苏尔=”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,类似Gecko)Chrome/69.0.3497.100 Safari/537.36”
Add(“接受语言”,“zh-CN,zh;q=0.9,en;q=0.8”)
使用rep作为HttpWebResponse=xmlhttp.GetResponse()
将reps用作Stream=rep.GetResponseStream()
将streamreader用作streamreader=新的streamreader(reps,Encoding.GetEncoding(“UTF-8”))
msgbox(streamreader.ReadToEnd)
终端使用
终端使用
终端使用
捕获ex作为WebException
Dim代表为HttpWebResponse=DirectCast(例如,HttpWebResponse)

msgbox(rep Is Nothing)'如果您注释掉Try..Catch部分,它会给出什么错误消息?如果我注释掉Try..Catch部分,那么“使用rep作为HttpWebResponse=xmlhttp.GetResponse()”将抛出错误。因为这是404。你是说你想在HTTP响应状态代码为404时获取响应文本吗?是的,我想“通过HttpWebRequest”获取404响应,为什么不看看你得到的实际异常并从那里开始工作呢。将其视为例外。响应本身什么都没有