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;
}
}
}
}

HttpWebResponse Hangs Subsequent Connections
Athar.Iqbal
br = new BinaryReader((response.GetResponseStream()));
consider
Stream st = (response.GetResponseStream());
br = new BinaryReader(st);
...
st.Close();
Polypterus
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]
dannyR
NASWAY
Dragon123456789
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]
Paranth
Diester
Any other ideas
ramdee
What kind of connection is that (WiFi. GPRS ...)
Andre.Ziegler
Is there a specific bug that is preventing this, and if so, is there no workaround
elmoman
What was the NETCF version you used
Cheers,
Anthony Wong [MSFT]
Development
shameel
Anyone else offer thoughts
Allen Junior
LarsMohekan
>>
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]