开发者

httpwebresponse = request.getresponse freezes

开发者 https://www.devze.com 2023-01-30 19:14 出处:网络
I tried using suggestions from other posts for this and still no resolution. When sending a POST request, at times it will just begin to freeze and stall. I set up my code as follows and now basically

I tried using suggestions from other posts for this and still no resolution. When sending a POST request, at times it will just begin to freeze and stall. I set up my code as follows and now basically if I cannot figure out why it is freezing, then it needs to timeout, handle the exception and restart the function.

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
         ....

Does anyone know if this is a coding error or something else?

Ok figured out why its hanging...I am writing to the request and I get the error cannot close stream because its expecting more bytes even though I set the length of the content


Cant tell from the information given here, but if the url specified resides in a different domain than the location where this code is running, then it wont work.

See MSDN - Do a find on cross-domain

0

精彩评论

暂无评论...
验证码 换一张
取 消