Я только что нашел этот вопрос случайно. С помощью HttpClient вы можете получить ход загрузки (предполагая, что сервер отправляет заголовок длины содержимого). В следующем примере считывается возвращаемое содержимое в поток (файл) и вычисляется прогресс при загрузке. Важно то, что вы передаете HttpCompletionOption.ResponseHeadersRead, чтобы получить ответ, как только будет доступен любой контент, и отправлены заголовки.
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);
}
}
'HttpWebRequest' может модифицировать' Range' заголовка, используя [ 'AddRange()' метод] (http://msdn.microsoft.com/en -us/библиотека/f2cwk28s.aspx). – svick
Метод AddRange() только в .Net framework, а не winrt – troyou