views:

62

answers:

2

My application downloads huge files using HttpWebRequest -> WebResponse -> Stream -> FileStream. See code below.

With following the scenario we always get corrupted files:

  1. Start download.
  2. Unplug cable or click to pause download process.
  3. Close and open the application.
  4. Start download (it starts from the interruption point).
  5. Wait full file is downloaded.

Problem: the downloaded file is corrupted.

I'm sure this is common problem, but I failed googling or SOing it. Please advise. What can be the cause?

public class Downloader
{
    int StartPosition { get; set; }
    int EndPosition { get; set; }
    bool Cancelling { get; set; }

    void Download(string[] args)
    {
        string uri = @"http://www.example.com/hugefile.zip";
        string localFile = @"c:\hugefile.zip";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.AddRange(this.StartPosition);
        WebResponse response = request.GetResponse();
        Stream inputStream = response.GetResponseStream();

        using (FileStream fileStream = new FileStream(localFile, FileMode.Open, FileAccess.Write))
        {
            int buffSize = 8192;
            byte[] buffer = new byte[buffSize];
            int readSize;

            do
            {
                // reads the buffer from input stream
                readSize = inputStream.Read(buffer, 0, buffSize);

                fileStream.Position = this.StartPosition;
                fileStream.Write(buffer, 0, (int)readSize);
                fileStream.Flush();

                // increase the start position
                this.StartPosition += readSize;

                // check if the stream has reached its end
                if (this.EndPosition > 0 && this.StartPosition >= this.EndPosition)
                {
                    this.StartPosition = this.EndPosition;
                    break;
                }

                // check if the user have requested to pause the download
                if (this.Cancelling)
                {
                    break;
                }
            }
            while (readSize > 0);
        }
    }
}
+1  A: 

The AddRange() call is wrong, you want to pass a negative value so you'll get the remainder of the file. From the MSDN Library article:

If range is positive, the range is from the start of the data to range.
If range is negative, the range is from range to the end of the data.

I can't see how EndPosition is being initialized, that could be wrong too.

Hans Passant
I'm aware of that problem and I handle it in the real code. The code here is not the real one. It is stripped. Thanks.
Vasiliy Borovyak
No idea what that means. But the AddRange call is definitely wrong and explains the corruption.
Hans Passant
I'm using this workaround: http://www.codeguru.com/forum/showthread.php?t=467570
Vasiliy Borovyak
+1  A: 

To troubleshoot this I would suggest doing a file compare to determine what the differences are. Is the downloaded one missing a section? Does it have a duplicated section or does it have an incorrect section?

Martin Smith
The Beyond Compare tool showed that the server was broken, did not supported the Accept-Ranges header. Thanks, Martin!
Vasiliy Borovyak