3

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.

Nps
  • 1,638
  • 4
  • 20
  • 40

2 Answers2

1

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().

SteveFerg
  • 3,466
  • 7
  • 19
  • 31
  • Which Close method we are talking here? HttpWebRequest doesn't have Close method and HttpWebResponse not yet initialized here. – Nps Jun 27 '15 at 23:36
0

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
        }
    }
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Which Close method we are talking here? HttpWebRequest doesn't have Close method and HttpWebResponse not yet initialized here. – Nps Jun 27 '15 at 23:36
  • @Nps, check the documentation I have linked along with answer. Particularly you should call `Stream.Close()` or `HttpWebResponse.Close()`. See here https://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.close%28v=vs.110%29.aspx. – Rahul Jun 27 '15 at 23:39
  • But exception occurs in `GetResponse()` method which returns HttpWebResponse object which is not return due to exception. I can't call the Close method on null object. – Nps Jun 27 '15 at 23:42
  • @Nps, that's correct and calling close() is more of best practice. In this particular case, I don't see why even calling close() since it's likely be already closed. – Rahul Jun 27 '15 at 23:51
  • that was my question was, so the exception will automatically close the connection. We don't need to handle in this particular situation. – Nps Jun 28 '15 at 00:01
  • Yes, at least to me it looks like since there is no connection being established in reality. – Rahul Jun 28 '15 at 00:03
  • Sry I will have to go off now and I won't be able to help any further than what I have already. – Rahul Jun 28 '15 at 00:06
  • Added more detail in question – Nps Jun 28 '15 at 00:13