HttpWebRequest.BeginGetResponse() does not return the second time

Posted by evilfred on Stack Overflow See other posts from Stack Overflow or by evilfred
Published on 2010-06-16T18:27:42Z Indexed on 2010/06/16 18:32 UTC
Read the original article Hit count: 328

Filed under:
|
|

Hi,

I make one HttpWebRequest and call GetResponse() on it to get a synchronous response.

Then after processing that response, I make a new HttpWebRequest and call BeginGetResponse() on it. Since BeginGetResponse() is an asynchronous call I expect it to return right away, but it doesn't! Why not?

Here is some stripped down sample code:

        HttpWebRequest request = RequestFactory.MakeSessionCreationRequest();

        try
        {
            // Get the response from the server.
            using (WebResponse response = request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                   ; // Get the response.
                }
            }

            ; // Process the response.
        }
        catch (WebException e)
        {
            Logger("Caught WebException when attempting to connect: " + e);
            return;
        }

        // Make the second, asynchronous request.
        HttpWebRequest msgRequest = RequestFactory.MakeMessageRequest();
        IAsyncResult result = msgRequest.BeginGetResponse(
            new AsyncCallback(HandleResponse),
            msgRequest);

        // PROBLEM: This line is never reached!!!
        Logger("Message send started");

© Stack Overflow or respective owner

Related posts about c#

Related posts about asynchronous