Does Operation Timed out exception in httpWebRequest.GetResponse() method close the connection

1.1k views Asked by At

My question might be silly, but need an answer. As far as I know whenever "The Operation has timed out" exception occurs in HttpWebRequest.GetResponse() method than connection is closed and released. If it is not true than how does it work? I tried to google this but couldn't get the answer.

EDIT: In this case it was a post request, connection was established and the URL which called was processing the request at server end, but HttpWebRequest Object was waiting on response and after sometime exception occurred.

2

There are 2 answers

1
SteveFerg On

My understanding is that you must call the Close method to close the stream and release the connection. Failure to do so may cause your application to run out of connections. If you are uncertain, you can always put a try/catch block around the Close method or the HttpWebRequest.GetResponse().

8
Rahul On

Well I am not entirely sure but it looks like that the Operation TimedOut exception probably faults the underlying connection channel; cause all the request after that ends up with same exception.

Per MSDN Documentation

You must call the Close method to close the stream and release the connection. Failure to do so may cause your application to run out of connections.

I did a small trial to see

    private static void MakeRequest()
    {
        WebRequest req = null;
        try
        {
            req = WebRequest.Create("http://www.wg.net.pl");
            req.Timeout = 10;                
            req.GetResponse();
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.Message);
            req.Timeout = 10000;
            req.GetResponse(); // This as well results in TimeOut exception
        }
    }