none
HttpWebResponse close() hangs

    Question

  • There have been a few threads on this, but I can't find a resolution or workaround.  The HttpWebResponse  hangs on the third connection if you don't close it, or hangs on the first connection when you try to close it.

    public void Test()
    {
    HttpWebRequest  request  = (HttpWebRequest)
    WebRequest.Create(url);
           
    HttpWebResponse response = (HttpWebResponse)
    request.GetResponse() ;

    Console.WriteLine("Trying to close stream");
    response.Close();
    Console.WriteLine("Stream Closed");
    }

    if you comment out the Close(); you can get up to two connections.

    Anyone seen a workaround for this?

    Thanks
    Friday, December 22, 2006 8:30 PM

Answers

  • I think I found the answer.  I saw this here:

    http://dotnetslackers.com/community/blogs/ruslantrifonov/archive/2006/11/30/_2200_How-To_2200_-Series_3A00_-Detect-network-connection-in-Compact-Framework.aspx

    Anyway, you need to do a request.Abort() before closing the response. But you have to do it after you are done using the stream;

    This works:

    Thanks for helping!

    public void GetWebStream()
    {
    Console.WriteLine("trying to open then read from stream");

    try
    {
    HttpWebRequest  request  = (HttpWebRequest)
    HttpWebRequest.Create(url);

    request.KeepAlive = false;
    request.Timeout = 5000;

    HttpWebResponse resp =
    (HttpWebResponse)request.GetResponse();

    Console.WriteLine("response: {0}",
    resp.StatusDescription.ToString());

    Stream httpStream = resp.GetResponseStream();

    byte[] data = new byte[1000000];  //dummy read routine
    int remaining = data.Length;
    int offset = 0;

    while (remaining > 0)
    {
    int read = httpStream.Read(data, offset, remaining);
    if (read <= 0)
    throw new EndOfStreamException
    (String.Format("End of stream reached with {0} bytes left to read", remaining));
    remaining -= read;
    offset += read;
    }

    Console.WriteLine("Stream Read over -- close response");

    request.Abort();   // <=== MAGIC PART


    if (resp != null)
    resp.Close();

    Console.WriteLine("Stream Closed");


    }
    catch (Exception e)
    {
    Console.WriteLine("Failed: {0}", e.Message.ToString());
    httpStream = null;
    }
    return;
    }





    Wednesday, December 27, 2006 7:32 PM

All replies

  • Not having much luck with this one. Most of the examples on the web are the same, and there are some references that you need to close the response, or dispose of it. The CF doesn't have the dispose method. So I assume they are for the full framework. Anyway, I can't seem to use this HttpWebRequest/HttpWebResponse more than twice.

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

    HttpWebResponse response = HttpWebResponse)request.GetResponse();

    Stream testStream = request.GetRequestStream();
    // do something useful with testStream
    testStream.Flush();
    testStream.Close();
    response.Close();
    response = null;
    request = null;



    Sunday, December 24, 2006 6:24 PM
  • 1.0 or 2.0? if 2.0, have you tried applying SP1? http://msdn.microsoft.com/vstudio/support/vs2005sp1/default.aspx
    Tuesday, December 26, 2006 1:10 PM
  • I'm sorry, that would be useful.  1.0.  It's running on CE.NET 4.2, Xscale, but the problem is also there in the emulator, so it looks like it's the compact framework (although I haven't ruled out pilot error yet) rather than the OS or the processor.

    I was going to try to put the HttpWebRequest/Response in it's own object/instance.  Maybe Disposing of the object will take the HttpWebRequest with it.  It's a stretch. 

    I'm surprised there are no one has posted how to do this. If you google "HttpWebRequest close hangs", others have seem this.  It's seems to be a pretty core method of getting data over a network.  Anyway, I can't think of another way to do it right now, so I'll keep trying the HttpWebRequest path.
    Wednesday, December 27, 2006 3:37 PM
  • I'm assuming you've probably already come across this?

     

    http://blogs.msdn.com/feroze_daud/archive/2004/01/21/61400.aspx

     

    Hi!

    There are many reasons why GetResponse() might hang. Some are:

    1) You have reached the connection limit on the client ( 2 conns per http/1.1 server, 4 connections per http/1.0 server, or custom limit set on the ServicePoint), and no connection is free to send a request to the server. In other words, you might have 2 requests outstanding (eg: to a 1.1 server) which are taking more than 2 minutes to complete, and then issue another GetResponse() to the same server. The last getresponse() might time out.

    2) You have not closed the response stream of an earlier httpwebresponse. So, even if a connection is free, the request wont be sent.

    3) The server is taking too long to send the response.

    The best way to debug this is to use a network sniffer (for eg: NetworkMonitor) which ships with Windows NT (if you are running on NT platforms). Or you can use free sniffers which are available on the web.

    Wednesday, December 27, 2006 3:46 PM
  • Thanks ab.  Yeah, I saw that post as well.  The GetReponse hangs after the second request.  I haven't confirmed the 2 conns max, but it sure fits.

    I think I've made some progress, even though it's still pretty screwed up.
    I did notice trying to make the 3rd request doesn't hang forever.  It used the Timeout property set to a 100 second timeout.  So there are 2 open requests, and it can't make the third one.  So you apparently need to close one first.

    But if you use the HttpWebResponse.Close method, it will hang indefinitely.  I just need to figure out how to close the response so I can make another request.
    Wednesday, December 27, 2006 6:46 PM
  • if you start everything clean -- web publishing service just restarted, no prior calls from the client -- can you successfully, repeatedly, make a single request, get the response, and close it?
    Wednesday, December 27, 2006 7:01 PM
  • Hello Andrew,

    I can successfully and repeatedly make a single request, but I can't close the response.



    try
    {
    HttpWebRequest  request  = (HttpWebRequest)
    HttpWebRequest.Create(url);

    request.KeepAlive = false;

    request.Timeout = 5000;

    HttpWebResponse resp = (HttpWebResponse)request.GetResponse();

    Console.WriteLine("response: {0}",
    resp.StatusDescription.ToString());   //comes back with "OK"

    if (resp != null)

    resp.Close();

    Console.WriteLine("Response Closed");
    }
    catch (Exception e)
    {
    Console.WriteLine("Failed: {0}", e.Message.ToString());
    httpStream = null;
    }

    Wednesday, December 27, 2006 7:22 PM
  • I think I found the answer.  I saw this here:

    http://dotnetslackers.com/community/blogs/ruslantrifonov/archive/2006/11/30/_2200_How-To_2200_-Series_3A00_-Detect-network-connection-in-Compact-Framework.aspx

    Anyway, you need to do a request.Abort() before closing the response. But you have to do it after you are done using the stream;

    This works:

    Thanks for helping!

    public void GetWebStream()
    {
    Console.WriteLine("trying to open then read from stream");

    try
    {
    HttpWebRequest  request  = (HttpWebRequest)
    HttpWebRequest.Create(url);

    request.KeepAlive = false;
    request.Timeout = 5000;

    HttpWebResponse resp =
    (HttpWebResponse)request.GetResponse();

    Console.WriteLine("response: {0}",
    resp.StatusDescription.ToString());

    Stream httpStream = resp.GetResponseStream();

    byte[] data = new byte[1000000];  //dummy read routine
    int remaining = data.Length;
    int offset = 0;

    while (remaining > 0)
    {
    int read = httpStream.Read(data, offset, remaining);
    if (read <= 0)
    throw new EndOfStreamException
    (String.Format("End of stream reached with {0} bytes left to read", remaining));
    remaining -= read;
    offset += read;
    }

    Console.WriteLine("Stream Read over -- close response");

    request.Abort();   // <=== MAGIC PART


    if (resp != null)
    resp.Close();

    Console.WriteLine("Stream Closed");


    }
    catch (Exception e)
    {
    Console.WriteLine("Failed: {0}", e.Message.ToString());
    httpStream = null;
    }
    return;
    }





    Wednesday, December 27, 2006 7:32 PM
  • glad to hear you found an answer ... that's the direction I was heading, you can't leave an open request hanging out there, THAT is the connection, as seen by the server. a server responds to a request ... your response is just the local instance of the push back from the server. as long as the request is open, that connection resource is tied up on the server side.
    Wednesday, December 27, 2006 7:37 PM