Writing file from HttpWebRequest periodically vs. after download finishes?

Posted by WB3000 on Stack Overflow See other posts from Stack Overflow or by WB3000
Published on 2010-04-18T20:49:40Z Indexed on 2010/04/18 20:53 UTC
Read the original article Hit count: 354

Filed under:
|
|

Right now I am using this code to download files (with a Range header). Most of the files are large, and it is running 99% of CPU currently as the file downloads. Is there any way that the file can be written periodically so that it does not remain in RAM constantly?

private byte[] GetWebPageContent(string url, long start, long finish)
    {
        byte[] result = new byte[finish];
        HttpWebRequest request;

        request = WebRequest.Create(url) as HttpWebRequest;

        //request.Headers.Add("Range", "bytes=" + start + "-" + finish);

        request.AddRange((int)start, (int)finish);

        using (WebResponse response = request.GetResponse())
        {
            return ReadFully(response.GetResponseStream());
        }

    }

public static byte[] ReadFully(Stream stream)
    {
        byte[] buffer = new byte[32768];
        using (MemoryStream ms = new MemoryStream())
        {
            while (true)
            {
                int read = stream.Read(buffer, 0, buffer.Length);
                if (read <= 0)
                    return ms.ToArray();
                ms.Write(buffer, 0, read);
            }
        }
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about httpwebrequest