HttpWebRequest.GetResponse() hangs the second time it is called

后端 未结 5 742
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 00:43

I\'m trying to fetch a series of files via HTTP, using HttpWebRequest. The first request goes through fine, but the second time through the same code GetResponse() hangs and

相关标签:
5条回答
  • 2020-12-10 01:23

    Plase try use:

             HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
             httpWebRequest.CachePolicy = noCachePolicy;
    
    0 讨论(0)
  • 2020-12-10 01:29

    Make sure you are creating a NEW HttpWebRequest each time. Quote from the MSDN reference on GetResponse method :

    Multiple calls to GetResponse return the same response object; the request is not reissued.

    Update 1: Ok. How about if you try closing the webResponseStream too - you currently don't do that, you're only closing the webResponse (just trying to rule things out). I'd also dispose of the httpWebResponse too (not just the WebResponse)

    Update 2: The only other thing I can suggest is to have a look at the following articles that I looked at when I did something similar to what you're doing:
    http://arnosoftwaredev.blogspot.com/2006/09/net-20-httpwebrequestkeepalive-and.html
    http://www.devnewsgroups.net/dotnetframework/t10805-exception-with-httpwebrequest-getresponse.aspx

    The only noticeable things I do different are:
    - set webrequest.KeepAlive = false
    - I don't have the connection management stuff in the config (I don't loop round to make a series of requests)

    0 讨论(0)
  • 2020-12-10 01:39

    'You need to initialize the webrequest and webresponse objects with a valid URI:

            Dim request As WebRequest = WebRequest.Create("http://google.com")
            Dim response As WebResponse = request.GetResponse()
    
            Function UrlExists(ByVal URL As String) As Boolean
                Try
                    request = WebRequest.Create(URL)
                    response = request.GetResponse()
                Catch ex As Exception
                    Return False
                Finally
                    response.Close()
                End Try
                Return True
            End Function
    
    0 讨论(0)
  • 2020-12-10 01:41
    httpWebRequest.Abort(); // before you leave
    

    Will solve!

    Take a look at the resource.

    0 讨论(0)
  • 2020-12-10 01:45

    Documentation for HttpWebRequest.ContentLength says:

    Any value other than -1 in the ContentLength property indicates that the request uploads data and that only methods that upload data are allowed to be set in the Method property.

    After the ContentLength property is set to a value, that number of bytes must be written to the request stream that is returned by calling the GetRequestStream method or both the BeginGetRequestStream and the EndGetRequestStream methods.

    I suspect it hangs because you're setting the content length but then not uploading any data. One would think that it should throw an exception in that case, but apparently it doesn't.

    0 讨论(0)
提交回复
热议问题