HttpWebResponse Hangs Subsequent Connections

Greetings,

I am having issues with a method I wrote for a compact framework application I wrote. The method is supposed to download an image from a specified URL (functionality easily present in the full framework, but not the Compact one unfortunately). My method works fine on its first trip out, but subsequent trips cause it to hang my application until the request times out.

The workflow for this method is that it is called for each photo retrieved from a Flickr.com API call. 

The code is below.

      public static Image LoadImageFromURL(string URL) 
      { 
         HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(URL); 
         HttpWebResponse response = null; 
         BinaryReader br = null; 
         MemoryStream stream = null; 
         request.Timeout = 10000;   // 10 Seconds 
         try 
         { 
            int tmp = ServicePointManager.DefaultConnectionLimit; 
            response = (HttpWebResponse) request.GetResponse();  // i lock up on the second method call 
       
            br = new BinaryReader((response.GetResponseStream())); 
    
            byte[] buffer = new byte[256]; 
            stream = new MemoryStream(); 
    
            int length =0; 
    
            do 
            { 
               length = br.Read(buffer,0,256); 
               stream.Write(buffer,0,length); 
            } 
            while (length >0); 

            request.Abort();
            request = null; 
            response.Close(); 
            response = null; 
            return (Image)new Bitmap(stream); 
             
         } 
         catch(Exception e) 
         { 
            MessageBox.Show(e.Message.ToString()); 
            return null; 
         } 
         finally 
         { 
            if (response != null) 
            { 
               response.Close(); 
               response = null; 
            } 
         } 
      } 

   }


Answer this question

HttpWebResponse Hangs Subsequent Connections

  • jony67907

    Another thing I wanted to point out is that if by any chance your code ever gets into that exception handler you should a) close your webresponse object and b) if the Exception is WebException, get a WebResponse from it (one of the members of exception class) and close it before leaving catch block

  • Sivakumar

    I am still having the same issues after using the above code.  It still times out on the second fetch attempt, and subsequent ones after.

    Anyone else offer thoughts

  • Prakash Ghatage

    This article states that there's an issue when its not used, so I was just following it's advice.  I removed the line from my code, and still have the same timeout issues.  Very frustrating.

  • Robuk

    >>

    : Another thing I wanted to point out is that if by any chance your code ever gets into that exception handler you should a) close your webresponse object and b) if the Exception is WebException, get a WebResponse from it (one of the members of exception class) and close it before leaving catch block


    For b), you should cast the WebResponse to HttpWebResponse, and then call HttpWebResponse.GetResponseStream(). If the return value is not a null reference, you should close the stream retrieved.

    As a last resort, you may increase the value of ServicePointManager.DefaultConnectionLimit.

    Cheers,
    Anthony Wong [MSFT]


  • mrobey1

    I am producing a test case using CF2 and VS2005, but I should note that I need this to work with the 1.0 framework because of a Pretec camera driver I am using to capture photos is not compatible with PocketPCs newer than version 2002 (and Version 2 of the Framework only targets 2003 and above).  This is a final project for a mobile development course, and I am somewhat bound by that restriction.  :-)

    Is there a specific bug that is preventing this, and if so, is there no workaround

  • Shortty

    Hi tikki,

    What was the NETCF version you used

    Cheers,
    Anthony Wong [MSFT]

  • ARichter

    Instead of  
    br = new BinaryReader((response.GetResponseStream()));
    consider
    Stream st = (response.GetResponseStream());
    br = new BinaryReader(st);
    ...
    st.Close();

  • Andrey Nikovskiy

    What kind of connection is that (WiFi. GPRS ...)


  • waltercjohnson

    Hi tikki,

    Could you try if your issue would reproduce on NETCF V2 There are bugs in V1 SP3 that has been fixed in CF V2.

    Cheers,
    Anthony Wong [MSFT]

  • Sandro Franchi

    Right now its just the Pocket PC 2003 Emulator (with Internet connection enabled and functioning).  I am using WiFi on the host machine for the net connection.  I haven't tested it on a device yet.

  • troy123

    Hi tikki,

    I noticed you call Request.Abort() in the code. You shouldn't need to do that. Why was that call needed

    Cheers,
    Anthony Wong [MSFT]

  • CHR1S

    CF 1.0 SP3

  • pathetic_noob

    I set it to specifically catch the WebException and its still giving me the same timeout errors as before when trying to retrieve the second item or subsequent ones after it.

    Any other ideas

  • James Hill

    I created a sample project and built it against Compact Framework 2.0 and the issue is not there, so I suppose its just isolated to 1.0.

  • HttpWebResponse Hangs Subsequent Connections