0

我在这里有一个有效的 HttpWebRequest 代码,但我的问题是即使我将指定的网站处于脱机状态,它仍然会继续执行 WebRequest,这意味着即使请求从未真正发生过,它仍然会继续发出 webrequests。

这是我的代码:

            Dim cweb As String = "http://samplewebsiteform.com"
    Dim POST As String = "name=TestName&age=50"
            Dim request As HttpWebRequest

                request = CType(WebRequest.Create(cweb), HttpWebRequest)
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36"
                request.AllowAutoRedirect = False
                request.ContentType = "application/x-www-form-urlencoded"
                request.ContentLength = POST.Length
                request.Method = "POST"
                request.KeepAlive = False
                request.Timeout = 500

                Dim requestStream As Stream = request.GetRequestStream()
                Dim postBytes As Byte() = Encoding.ASCII.GetBytes(POST)
                requestStream.Write(postBytes, 0, postBytes.Length)
                requestStream.Close()

每当它尝试使用离线网站进行网络请求时,我如何捕获此网络请求,以便它停止发出请求?

4

1 回答 1

1

在请求请求流之前您没有收到响应。

这一行:

 Dim resphttp As HttpWebResponse = CType(HttpWebResponse, request.GetResponse)

将允许您获取 Web 响应状态代码(404 未找到,500 错误...)

 If resphttp.StatusCode <> Net.HttpStatusCode.Accepted Then
        'There was an error
 End If

在请求响应后,您将获得 requestStream:

 Dim requestStream As Stream = request.GetRequestStream()
于 2013-10-03T08:26:47.920 回答