简体   繁体   English

使用C#Httpclient从异步GET获取下载进度/当前下载的字节

[英]Getting download progress / currently downloaded bytes from an async GET with C# Httpclient

I'm trying to get some download progress represented in Xamarin using System.Web.Http's httpclient. 我正在尝试使用System.Web.Http的httpclient在Xamarin中获得一些下载进度。 I understand in the typical case to get the file size in advance with a HEAD request and checking for the content-length of the file in question. 我了解在典型情况下,可以通过HEAD请求提前获取文件大小并检查相关文件的内容长度。 What I don't understand is how to get the currently transferred file size so I can compare that against the total. 我不了解如何获取当前传输的文件大小,以便将其与总数进行比较。

Is this possible? 这可能吗? I've looked around for other examples, but they seem to be making use of WebClient or other such instead. 我到处寻找其他示例,但是它们似乎在利用WebClient或其他类似示例。

Edit: 编辑:

submitButton.Clicked += async (object sender, EventArgs e) => {
var client = new HttpClient();

var url = (Just making use of a sizable wallpaper here as example.);

var result = await client.GetStreamAsync(url);
}

If I await the GetStreamAsync as above, then I can't perform any operations on the stream until it's done. 如果我等待上述的GetStreamAsync,则直到完成后,我才能对流执行任何操作。 If I remove the await and try to work with the Task result, the same thing takes place - even trying to get a Length won't work until the entire thing is completed anyway. 如果我删除等待并尝试使用Task结果,则将发生相同的事情-即使尝试获取Length都无法完成,直到整个事情都完成了。

Obviously if I can get at the stream as it's being written to, I can check the length (hopefully) and just compare that to the header information. 显然,如果我能像正在写入的流中那样获得流,则可以(希望)检查长度并将其与标头信息进行比较。 But how to access that mid-transfer, I do not get. 但是我不知道如何进行中间转移。

Unfortunately, the best solution I've been able to find for this so far is "use Webclient instead of httpclient". 不幸的是,到目前为止我能找到的最好的解决方案是“使用Webclient而不是httpclient”。 I haven't been able to give Webclient a shot yet in Xamarin, and hopefully it will work there, but I managed to get progress working with it on the PC in a straightforward manner. 我还无法在Xamarin中向Webclient展示一下,希望它可以在其中运行,但是我设法以简单的方式在PC上使用它取得了进展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM