实现在 .net 中使用 HttpClient 下载文件时显示进度
在 .net framework 中,要实现下载文件并显示进度的话,最简单的做法是使用 WebClient 类。订阅 DownloadProgressChanged 事件就行了。
但是很可惜,WebClient 并不包含在 .net standard 当中。在 .net standard 中,要进行 http 网络请求,我们用得更多的是 HttpClient。另外还要注意的是,UWP 中也有一个 HttpClient,虽然用法差不多,但是命名空间是不一样的,而且 UWP 的是可以支持获取下载进度的,这里就不再细说。
如果要下载文件,我们会使用到 HttpClient 的 GetByteArrayAsync 这个方法。要实现下载进度,那要怎么办呢?俗话说,不行就包一层。这里我们写个扩展方法,定义如下:
public static class HttpClientExtensions
{
public static Task<byte[]> GetByteArrayAsync(this HttpClient client, Uri requestUri, IProgress<HttpDownloadProgress> progress, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
其中 HttpDownloadProgress 是我自己定义的结构体(不使用类的原因我下面再说),代码如下:
public struct HttpDownloadProgress
{
public ulong BytesReceived { get; set; } public ulong? TotalBytesToReceive { get; set; }
}
BytesReceived 代表已经下载的字节数,TotalBytesToReceive 代表需要下载的字节数,因为 http 的响应头不一定会返回长度(content-length),所以这里设置为可空。
由于我们需要从 http 响应头获取到 content-length,而 HttpClient 自身的 GetByteArrayAsync 并没有办法实现,我们需要转向使用 GetAsync 这个方法。GetAsync 这个方法有一个重载 https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.httpclient.getasync#System_Net_Http_HttpClient_GetAsync_System_Uri_System_Net_Http_HttpCompletionOption_System_Threading_CancellationToken_ 它的第二个参数是一个枚举,代表是什么时候可以得到 response。按照需求,我们这里应该使用 HttpCompletionOption.ResponseHeadersRead 这个。
另外 HttpClient 的源码也可以在 Github 上看得到。https://github.com/dotnet/corefx/blob/d69d441dfb0710c2a34155c7c4745db357b14c96/src/System.Net.Http/src/System/Net/Http/HttpClient.cs 我们可以参考一下 GetByteArrayAsync 的实现。
经过思考,可以写出下面的代码:
public static class HttpClientExtensions
{
private const int BufferSize = ; public static async Task<byte[]> GetByteArrayAsync(this HttpClient client, Uri requestUri, IProgress<HttpDownloadProgress> progress, CancellationToken cancellationToken)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
} using (var responseMessage = await client.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false))
{
responseMessage.EnsureSuccessStatusCode(); var content = responseMessage.Content;
if (content == null)
{
return Array.Empty<byte>();
} var headers = content.Headers;
var contentLength = headers.ContentLength;
using (var responseStream = await content.ReadAsStreamAsync().ConfigureAwait(false))
{
var buffer = new byte[BufferSize];
int bytesRead;
var bytes = new List<byte>(); var downloadProgress = new HttpDownloadProgress();
if (contentLength.HasValue)
{
downloadProgress.TotalBytesToReceive = (ulong)contentLength.Value;
}
progress?.Report(downloadProgress); while ((bytesRead = await responseStream.ReadAsync(buffer, , BufferSize, cancellationToken).ConfigureAwait(false)) > )
{
bytes.AddRange(buffer.Take(bytesRead)); downloadProgress.BytesReceived += (ulong)bytesRead;
progress?.Report(downloadProgress);
} return bytes.ToArray();
}
}
}
}
这里我将缓冲区设置为 8192 字节(8 KB),相当于每读取 8 KB 就汇报一次下载进度,当然各位看官也可以把这个值调小,这样效果会更好,但相对的性能就差一些。同时也因为这里 Report 的频率是比较高的,因此 HttpDownloadProgress 不适合用 class(否则 GC 会压力相当大)。
下面我自己的 Demo 的效果图:
实现在 .net 中使用 HttpClient 下载文件时显示进度的更多相关文章
- dotnet 通过 HttpClient 下载文件同时报告进度的方法
本文告诉大家一个简单的方法通过 HttpClient 下载文件,同时报告下载进度 通过 HttpClient 的 ContentLength 很多时候都可以拿到下载的内容的长度,通过 ReadAsyn ...
- 使用libcurl开源库和Duilib做的下载文件并显示进度条的小工具
转载:http://blog.csdn.net/mfcing/article/details/43603525 转载:http://blog.csdn.net/infoworld/article/de ...
- [c#]WebClient异步下载文件并显示进度
摘要 在项目开发中经常会用到下载文件,这里使用winform实现了一个带进度条的例子. 一个例子 using System; using System.Collections.Generic; usi ...
- C# Winform下载文件并显示进度条
private void btnDown_Click(object sender, EventArgs e) { DownloadFile("http://localhost:1928/We ...
- Winform下载文件并显示进度条
本来是要研究怎样判断下载完成,结果找到这个方法,可以在这个方法完成之后提示下载完成. 代码如下: using System; using System.Collections.Generic; usi ...
- 通过HttpUrlConnection下载文件并显示进度条
实现效果: 核心下载块: int count = 0; URL url = new URL("http://hezuo.downxunlei.com/xunlei_hezuo/thunder ...
- 在WebView中加载HTML页面时显示进度对话框的方法
webView.setWebViewClient(new WebViewClient(){ ProgressDialog prDialog; @Overri ...
- android中使用Http下载文件并保存到本地SD卡
1.AndroidMainfest.xml中设置权限 <uses-permission android:name="android.permission.INTERNET"& ...
- C#中解决Response.AddHeader("Content-Disposition", "attachment; filename=" + filename)下载文件时文件名乱码的问题
问题:下载文件时文件名乱码怎么解决? 在C#写后台代码过程中,经常遇到下载文件出现文件名乱码的问题,在网上找了很多方法,总是存在浏览器不兼容的问题,当IE浏览器不乱码时,火狐浏览器就会乱码,后来经过反 ...
随机推荐
- SpringMVC,采用的是SpringJDBC
上一次复习搭建了SpringMVC+Mybatis,这次搭建一下SpringMVC,采用的是SpringJDBC,没有采用任何其他的ORM框 架,SpringMVC提供了一整套的WEB框架,所以如果想 ...
- 51NOD——N 1107 斜率小于0的连线数量
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1107 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 ...
- windows下, nginx 提示错误 "No input file specified"
https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80386035
- stm32的adc时钟周期,ADC_SampleTime_1Cycles5是多长时间
- Mybatis全面详解——上(学习总结)
原文地址:https://blog.csdn.net/ITITII/article/details/79969447 一.什么是Mybatis 这里借用官网的一句话介绍什么是mybatis:MyBat ...
- php框架排名(Laravel一直第一)
php框架排名(Laravel一直第一) 一.总结 1.Laravel,后面就用这个框架(要用好这个框架,英语得6啊) 2.YII框架和tp框架一样,也是一个国产框架 二.2017世界PHP框架排名T ...
- [Docker] Run Short-Lived Docker Containers
Learn the benefits of running one-off, short-lived Docker containers. Short-Lived containers are use ...
- pandas 学习(五)—— datetime(日期)
date range pd.date_range('2014-11-19', '2014-11-21', freq='D') # 起始时间,终止时间,时间间隔,也即步长,D ⇒ Day,5H:以 5 ...
- 【25.64%】【codeforces 570E】Pig and Palindromes
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- .NET-架构优化实战-梳理篇
原文:.NET-架构优化实战-梳理篇 前言 程序员输出是他敲写的代码,那么输入就是他思考好的设计.因此不做设计是不存在,设计只分优秀的设计和糟糕的设计.为了避免过度设计浪费成本,需要针对现有业务与问题 ...