The operation has timed out
Friday, November 13, 2009 at 12:41AM Actually, it said 'System.Net.WebException: The operation has timed out'... and it said it a bunch of times. I swear the error log was laughing at me.
Working in C# (.Net 3.5) tonight and had to write a method that contacted a few other sites and downloaded some data. Simple enough, just loop through the urls, setup the request, get the response and process.
foreach(var url in urls)
{
var request = WebRequset.Create(url);
var response = request.GetResponse();
//... do something with the response
response.close();
}
Worked like a charm for the first run, so I let it go. Came back 15 minutes later to a log file full of errors telling me that 'The operation has timed out.' So, with just a little bit of research I found this post which points out that without a call to request.Abort() you start running into this problem.
So, I added a finally{} block with a request.Abort() in it and everything's been fine. I have to admit, it feels like a bit of a hack, so I think I'm going to have to do some more research. Will update here when I know more.
Update:
I found a post on a form that mentioned that setting the HttpWebRequest.KeepAlive might be an issue. According to the post, when it is true (it's default) it keeps a connection to the remote Url in a connection pool, so that it can quickly hit it again if it has to. And, if the remote server's keepalive is set to a shorter interval it will close the connection before the WebRequset does, so when the WebRequest tries to use the same request again it throws because it thinks it's getting an open connection to the server.
I tried commenting out the request.Abort() and adding ((HttpWebRequest)request.KeepAlive = false; but ran into the same exact issue about 90 seconds into my tests. So I'm putting the Abort() hack back in until I get some better info.
Rob Kitson |
1 Comment |