使用HttpClient的进度信息
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用HttpClient的进度信息相关的知识,希望对你有一定的参考价值。
我想在Windows Phone 8.1 Silverlight中使用“Windows.Web.Http.HttpClient”实现进度条。
编辑:第一次尝试:
private async Task CheckProgress()
{
var df = httpClient.GetAsync(new Uri(uriString, UriKind.Absolute)).Progress = Progress;
// here i want to stop the execution till whole content downloaded
// Reason to wait here
// this client download will be used by one more async method. so i need to wait here till the file completely downloaded.
// this whole is done in this method only ( requirement)
}
private void Progress(IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> asyncInfo, HttpProgress progressInfo)
{
// here i getting the progress value.
// i have already set a call back method that report the progress on the UI.
}
还有一次尝试:但获得例外Invocation of delegate at wrong time.
private async Task CheckProgress()
{
var df = httpClient.GetAsync(new Uri(uriString, UriKind.Absolute))
df.Progress = Progress;
await df;
// here i want to stop the execution till whole content downloaded
}
题:
所有,我想要停止CheckProgress()
方法等待整个下载完成。
答案
我得到了我的问题的解决方案,但在我的Second Attempt
只是有点想念。
private async Task CheckProgress()
{
var df = httpClient.GetAsync(new Uri(uriString, UriKind.Absolute))
df.Progress = (res, progress) =>
{
// no separate event handler.
}
await df;
}
它工作正常。希望它会帮助别人欢呼:)
以上是关于使用HttpClient的进度信息的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Apache HttpClient 4 获取文件上传的进度条?
Android学习笔记--使用Apache HttpClient实现网络下载效果,附带进度条显示
实现在 .net 中使用 HttpClient 下载文件时显示进度
如何使用 C# HttpClient PostAsync 显示上传进度