HttpRequest.GetResponse() call fails unexpectedly
This is a lesson learned long ago. Before the proliferation of webservice calls I needed to do currency conversion on a website. I made a request to the Yahoo! finance page and would parse the html looking for the values that a user would see and using them in my application. Everything worked well in staging, but once it went to production, I was receiving "The underlying connection was closed: Unable to connect to the remote server" exceptions. The only way to fix it was to recycle the app pool.
I looked into it further and by default .NET will pool the web connection. Even when you use WebRequest.Create( url ) it wanted to reuse an existing connection. That would work well if the volume was high, but after a short period of inactivity, yahoo's web server would break the connection. That is quite expected and if I was running a high volume site, I would want to close out as many inactive connections as I could.
There is a property on the HttpWebRequest object that will instruct the framework to not pool your remote connections.
Here is the standard code snippet I use when making a outgoing request to a page. This code is for a GET, but should work well in a POST.
HttpWebRequest request = ( HttpWebRequest ) WebRequest.Create( url );
request.KeepAlive = false;
using ( WebResponse response = request.GetResponse() )
{
using ( StreamReader reader = new StreamReader( response.GetResponseStream() ) )
{
return reader.ReadToEnd();
}
}
I hope this will help someone track down unexpected connection closes.