views:

572

answers:

1

I have an application that makes a sync request on an external resource via http(s). Every few weeks I get the following exception

Exception: System.Net.WebException - Message: Unable to connect to the remote server

Inner Exception: System.Net.Sockets.SocketException - Message: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

What is bothering me is that my trace logs show that for every single instance of this exception, it was thrown "21 seconds" into the request.

Has anybody seen behaviour like this before and is there a solution? Are there any known issues within the System.Net.HttpWebRequest namespace? What are the best practices for using System.Net.HttpWebRequest e.g. should I explicitly close and dispose of response streams...

A: 

If you're accessing external resources, the answer is most likely that those resources weren't available (their servers were down, your connection was down, their connection was down, etc).

You can set the .Timeout property before the GetResponse() method of the HTTPWebRequest object to extend the time before it officially times out and see what happens:

myHttpWebRequest.Timeout = 10; //In milliseconds

Failing this, you could try adding this to your configuration file if your running ASP.NET with NETWORK SERVICE as your host account:

<configuration >
  <system.net>
    <defaultProxy>
      <proxy bypassonlocal="true" usesystemdefault="false" />
    </defaultProxy>
</system.net>
</configuration>

If you don't, your application can't get to the proxy settings. .NET search for it, can't find it and then connects via default. This will cause the exception but the excution will continue. It may explain why it's always 21 seconds into a request.

Read the full details here.

Mr. Smith
It's not timing out. The default timeout is 100 seconds. My request is reaching the remote server being processed and everything is working as expected, but I get the above mentioned exceptions?
Updated my answer, hope it helps.
Mr. Smith