java httpclient 进度条,与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 cant use DownloadOperation(Background transferring) because data from server requests certificate - and this functionality doesn't exist in DownloadOperations.

解决方案

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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值