Download a file given an url to a restricted area and a cookie container I have a queue of jobs in an ASP.NET CORE Application. Each of the files are in a restricted area and I have to Login first, thats why I use a Cookie Container for my download request.

The Job is logging into a platform and then starts to download the file, given an URL and a Cookie Container from the Login. 

The download method is looking like this and has a Func which reports the progress in 5 % steps:

public async Task DownloadFile(string url, string destination, Func&lt;int, Task&gt; reportStatus)
{
        await reportStatus(0);

        var request = WebRequest.CreateHttp(url);
        request.CookieContainer = m_cookieContainer;
        var response = (HttpWebResponse)await request.GetResponseAsync();
        var requestStream = response.GetResponseStream();

        using (var reader = new BinaryReader(requestStream))
        {
            using (var fileStream = File.OpenWrite(destination))
            {
                const int bufferSize = 2048;
                byte[] bytes;
                short oldPercentage = 4;
                long bytesRead = 0;
                do
                {
                    bytes = reader.ReadBytes(bufferSize);
                    await fileStream.WriteAsync(bytes, 0, bytes.Length);

                    bytesRead += bytes.Length;

                    var percentage = (short)(bytesRead * 100 / response.ContentLength);
                    if (oldPercentage &lt; percentage)
                    {
                        await reportStatus(percentage);
                        oldPercentage = (short)(oldPercentage + 5);
                    }
                } while (bytes.Length != 0);
            }
        }

        await reportStatus(100);
}


Small problems I am currently facing:


Download speed feels quite slow in comparison to my internet connection.
I was wondering if there are better ways downloading a file from a restricted area using .NET Core functionality.
You mentioned that the download is slower than you'd expect, I'll focus on just that for a quick review.

Look at your code here:


var percentage = (short)(bytesRead * 100 / response.ContentLength);
if (oldPercentage &lt; percentage)
{
    await reportStatus(percentage);
    oldPercentage = (short)(oldPercentage + 5);
}



Can you see why that might be slowing down your download?


await reportStatus(percentage);



You're waiting for the task reporting the status to finish before reading the rest of the stream! What if reportStatus were logging the progress to a database or doing something else really time consuming?

I think you should just execute the callback and not care when/how it finishes:

reportStatus(percentage); // Don't wait




Oh and oldPercentage is really confusing when you scan the code... I'd rename it to something like reportingThreshold.

if (percentage &gt; reportingThreshold)
{
     reportStatus(percentage);
     reportingThreshold = (short)(reportingThreshold+ 5);
}