Search code examples
c#windows-8downloadwindows-runtimewindows-8.1

Progress bar with HttpClient


I have a file downloader function:

HttpClientHandler aHandler = new HttpClientHandler();
aHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
HttpClient aClient = new HttpClient(aHandler);
aClient.DefaultRequestHeaders.ExpectContinue = false;
HttpResponseMessage response = await aClient.GetAsync(url);
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();

// To save downloaded image to local storage
var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
filename, CreationCollisionOption.ReplaceExisting);
var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite);
DataWriter writer = new DataWriter(fs.GetOutputStreamAt(0));

writer.WriteBytes(await response.Content.ReadAsByteArrayAsync());

await writer.StoreAsync();
//current.image.SetSource(randomAccessStream);
writer.DetachStream();
await fs.FlushAsync();

How can I realize progress bar functionality? Maybe I can get the writers bytes written so far? Or something?

P.S. I can't use DownloadOperation (Background transferring) because data from server requests certificate - and this functionality doesn't exist in DownloadOperations.


Solution

  • The best way to go is using Windows.Web.Http.HttpClient instead of System.Net.Http.HttpClient. The first one supports progress.

    But if for some reason you want to stick to the System.Net one, you will need to implement your own progress.

    Remove the DataWriter, remove the InMemoryRandomAccessStream and add HttpCompletionOption.ResponseHeadersRead to GetAsync call so it returns as soon as headers are received, not when the whole response is received. I.e.:

    // Your original code.
    HttpClientHandler aHandler = new HttpClientHandler();
    aHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
    HttpClient aClient = new HttpClient(aHandler);
    aClient.DefaultRequestHeaders.ExpectContinue = false;
    HttpResponseMessage response = await aClient.GetAsync(
        url,
        HttpCompletionOption.ResponseHeadersRead); // Important! ResponseHeadersRead.
    
    // To save downloaded image to local storage
    var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
        filename,
        CreationCollisionOption.ReplaceExisting);
    var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite);
    
    // New code.
    Stream stream = await response.Content.ReadAsStreamAsync();
    IInputStream inputStream = stream.AsInputStream();
    ulong totalBytesRead = 0;
    while (true)
    {
        // Read from the web.
        IBuffer buffer = new Windows.Storage.Streams.Buffer(1024);
        buffer = await inputStream.ReadAsync(
            buffer,
            buffer.Capacity,
            InputStreamOptions.None);
    
        if (buffer.Length == 0)
        {
            // There is nothing else to read.
            break;
        }
    
        // Report progress.
        totalBytesRead += buffer.Length;
        System.Diagnostics.Debug.WriteLine("Bytes read: {0}", totalBytesRead);
    
        // Write to file.
        await fs.WriteAsync(buffer);
    }
    inputStream.Dispose();
    fs.Dispose();