Httpwebrequest Times Out on Second Call

HttpWebRequest times out on second call

On the heels of the previous answers, I wanted to add a couple more things. By default HttpWebRequest allows only 2 connections to the same host (this is HTTP 1.1 "niceness"),

Yes, it can be overriden, no I won't tell you how in this question, you have to ask another one :)
I think you ought to look at this post.

I think that you are still not quite disposing of all your resources connected with the HttpWebRequest, so the connection pooling comes into play and that's the problem. I wouldn't try to fight the 2 connections per server rule, unless you really have to.

As one of the posters above noted, Fiddler is doing you a bit of a disservice in this case.

I'd add a nice finally {} clause after your catch and make sure that as the above post notes, all streams are flushed, closed and references to the request object are set to null.

Please let us know if this helps.

HttpWebRequest times out on the second call to the Web Server

I managed to fix the issue with disabling Expect100Continue header:

ServicePointManager.Expect100Continue = false;

According to the HTTP 1.1 protocol, when this header is sent, client requests that use the POST method expect to receive a 100-Continue response from the server to indicate that the client should send the data to be posted.

Having this property set to true (which is the default behavior of .Net) the data is not sent with the initial request. Instead, this header is sent to the web server which responds with 100-Continue if implemented correctly. However, not all web servers handle this correctly, including the server to which I am attempting to post data. I sniffed the headers using Fiddler and noticed that my code does send this header, while the Python Paste does not!

Second call to HttpWebRequest.GetRequestStream() throws timeout exception

This could be part of the problem:

private void InitCookies()
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create( baseUri );
req.Method = "GET";
req.CookieContainer = this.cookies;
req.GetResponse();
}

Here you're not disposing of the response... so it's not returning the connection to the pool.

It's not really clear what this method is meant to do, but you should dispose of the response:

using (req.GetResponse()) {}

HttpWebRequest.GetResponse() keeps getting timed out

I had the very same issue.
For me the fix was as simple as wrapping the HttpWebResponse code in using block.

using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
// Do your processings here....
}

Details: This issue usually happens when several requests are made to the same host, and WebResponse is not disposed properly. That is where using block will properly dispose the WebResponse object properly and thus solving the issue.



Related Topics



Leave a reply



Submit