我尝试使用其他帖子的建议,但仍然没有解决方案。发送POST请求时,有时它会开始冻结并停止。我按如下方式设置了我的代码,现在基本上如果我无法弄清楚为什么它会冻结,那么它需要超时,处理异常并重启函数。
For i As Integer = 0 To searchItems.Count - 1
If uri.Scheme = uri.UriSchemeHttps Then
Dim request As HttpWebRequest = HttpWebRequest.Create(url)
request.Method = "POST"
request.ContentLength = data.Length
request.ContentType = "application/xml; charset=utf-8"
request.Timeout = 20000
Dim writer As New StreamWriter(request.GetRequestStream)
Dim postBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(data)
writer.Write(postBytes)
***Using oResponse As HttpWebResponse = request.GetResponse()*** ' this is where it freezes
Dim reader As New StreamReader(oResponse.GetResponseStream())
responseData = reader.ReadToEnd()
reader.Dispose()
oResponse.Close()
End Using
request.Abort()
writer.Close()
End If
End If
Next
Catch e As WebException
If e.Status = WebExceptionStatus.ProtocolError Then
....
有人知道这是编码错误还是别的什么?
好的弄明白为什么它挂起......我正在写请求并且我得到错误无法关闭流,因为即使我设置了内容的长度它也期望更多的字节
答案 0 :(得分:0)
无法从此处给出的信息中判断,但如果指定的网址位于与运行此代码的位置不同的域中,则无法使用。