troyou
troyou

Reputation: 190

Download file in winrt, use HttpClient or HttpWebRequest or BackgroundDownloader?

I want download file by use HttpClient or HttpWebRequest or BackgroundDownloader.

Before I send the request, I need to modify the http header "Range" and “Cookie", and I want to get the download progress value

Now the problem is HttpClient can modify the "Range" header but cannot get the download progress. HttpWebRequest can get the progress of download but cannot modify the "Range" header. BackgroundDownloader cannot modify the "Cookie" header. This is a link "How to set cookie on BackgroundDownloader

What should I do?

Upvotes: 1

Views: 3785

Answers (3)

Rico Suter
Rico Suter

Reputation: 11868

I implemented very simple HTTP classes in my library. These classes support setting of cookies, headers and also IProgress for progress reporting (and more).

Check out the sample code on this site: https://mytoolkit.codeplex.com/wikipage?title=Http

Upvotes: 0

Jürgen Bayer
Jürgen Bayer

Reputation: 3023

I just found this question by chance. With HttpClient you can get a download progress (presuming the server sends the content-length header). The following example reads the returned content into a (file)stream and calculates the progress while downloading. One important thing is that you pass HttpCompletionOption.ResponseHeadersRead to get a response as soon as any content is available and headers were sent.

Uri uri = ...

// Request the data 
HttpResponseMessage responseMessage = await httpClient.GetAsync(uri,
   HttpCompletionOption.ResponseHeadersRead, cancellationToken);

// Get the size of the content
long? contentLength = responseMessage.Content.Headers.ContentLength;

// Create a stream for the destination file
StorageFile destinationFile = await destinationFolder
   .CreateFileAsync(destinationFileName,
   CreationCollisionOption.ReplaceExisting);
using (Stream fileStream =
   await destinationFile.OpenStreamForWriteAsync())
{
   // Read the content into the file stream
   int totalNumberOfBytesRead = 0;
   using (var responseStream =
      await responseMessage.Content.ReadAsStreamAsync())
   {
      int numberOfReadBytes;
      do
      {
         // Read a data block into the buffer
         const int bufferSize = 1048576; // 1MB
         byte[] responseBuffer = new byte[bufferSize];
         numberOfReadBytes = await responseStream.ReadAsync(
            responseBuffer, 0, responseBuffer.Length);
         totalNumberOfBytesRead += numberOfReadBytes;

         // Write the data block into the file stream
         fileStream.Write(responseBuffer, 0, numberOfReadBytes);

         // Calculate the progress
         if (contentLength.HasValue)
         {
            // Calculate the progress
            double progressPercent = (totalNumberOfBytesRead /
                (double)contentLength) * 100;

            // Display the progress     
             ...
         }
         else
         {
           // Just display the read bytes   
            ...
         }
      } while (numberOfReadBytes != 0);
   }
}

Upvotes: 2

Jan K.
Jan K.

Reputation: 2582

Under WinRT you can modify Message Headers with help of OperationContextScope. I'm not sure if it works with HttpClient, but with HttpWebRequest it does! See msdn article for an example.

Upvotes: 0

Related Questions