3

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

unohoo
  • 378
  • 4
  • 14
  • can I clarify: is the `ReadWriteTimeout` currently working? or not working? meaning: do you eventually get a `TimeoutException` (after 10 seconds?) – Marc Gravell Jan 23 '13 at 11:47
  • No, no timeout is thrown. I've left it for about 10 minutes and it still does not throw an exception. Also in this answer http://stackoverflow.com/questions/9140289/httpwebrequest-httpwebresponse-issues it is suggested that the receiveStream's ReadTimeout be set, however that throws an UnsupportedException ('timeouts are not supported on this stream') – unohoo Jan 23 '13 at 12:01

0 Answers0