I have the following method which is used to download a file. It works fine, unless the internet connection drops during the download process. In that case, it gets stuck on the receiveStream's read (ie in len = receiveStream.Read(buffer, 0, buffer.Length). It does not continue, or throw an exception, it just seems to be stuck inside there forever. I set the timeouts to 10s, and catch them higher up. Am I doing something wrong, or missing something?
I have tried using the async methods as well (BeginGetResponse, BeginRead etc), and they too get stuck.
I have tested this both on Windows mobile 6 professional emulator and a Windows mobile device.
private void DownloadFile(string url, string filename)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 10000;
request.ReadWriteTimeout = 10000;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream receiveStream = response.GetResponseStream())
{
long size = response.ContentLength;
using (Stream file = File.OpenWrite(filename))
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = receiveStream.Read(buffer, 0, buffer.Length)) > 0)
{
file.Write(buffer, 0, len);
total += len;
UpdateDownloadState(total, size);
}
}
}
}
}
Thanks