Is a pending exception thrown in the httpWebRequest.GetResponse () method to close the connection

My question may be silly, but an answer is needed. As far as I know, when " Operation Timeout ) occurs in the method HttpWebRequest.GetResponse()

, than the connection is closed and released. If this is not true, how does it work? I tried to do it but couldn't get a response.

EDIT: In this case, it was a post request, a connection was established, and the url that was calling was processing the request on the server, but the HttpWebRequest object was expecting a response and after some exception.

+3


source to share


2 answers


I understand that you have to call the Close method to close the stream and release the connection. Otherwise, your application may not work. When in doubt, you can always put a try / catch block around the Close

or method HttpWebRequest.GetResponse()

.



+1


source


Well, I'm not entirely sure, but it looks like the exception Operation TimedOut

is probably causing errors on the underlying connection channel; because the whole request after that ends up with the 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 end connections.



I did a little test 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
        }
    }

      

0


source







All Articles