HttpWebRequest times out on second call

后端 未结 8 534
猫巷女王i
猫巷女王i 2020-11-29 03:45

Why does the following code Timeout the second (and subsequent) time it is run?

The code hangs at:

using (Stream objStream = request.GetResponse().Ge         


        
相关标签:
8条回答
  • 2020-11-29 04:40

    As you have stated, running fiddler in the background would mitigate the issue. This is because fiddler force closes any responses. Extending on the above post from Sam B I would ensure that the response is closed like so:

    using (var wresponse = request.GetResponse())
    {
       using (Stream objStream = wresponse.GetResponseStream())
       {
            // ...
       } 
       wresponse.close();
    }
    

    Also it may be worth setting the proxy to null like so:

     request.Proxy = Null;
    

    As the .NET framework will go out searching for a proxy unless you explicitly do this. When fiddler is running this effect would be mitigated as fiddlers proxy would be found directly.

    0 讨论(0)
  • 2020-11-29 04:43

    The WebResponse obtained by request.GetReponse() MUST be disposed properly. Try this (removing request.Abort() and GC.Collect() calls):

    using (var wresponse = request.GetResponse())
    {
       using (Stream objStream = wresponse.GetResponseStream())
       {
            // ...
       }
    }
    

    Edit: Since it still does not work, I suggest you to test this with an empty windows application. This way, you could isolate app.config problems or maximum concurrent calls per host* (are you using other webrequest object somewhere else in your application to this host; which webresponse are not disposed properly?).

    Hope this solve your problem, I am out of ideas!

    • See Jon Skeet's answer here.
    0 讨论(0)
提交回复
热议问题