[点晴永久免费OA]C#利用HttpClient实现反向代理分块下载
|
admin
2022年7月5日 8:36
本文热度 1287
|
最近遇到的一个项目,附件在A服务器,对外是B服务器,A与B服务器可以内网通信,但是A服务器不对外,就需要用到B服务器的对外访问地址浏览附件与文件,参考了部分网上的代码,附上代码。
PS:不支持加速器下载
-
-
-
-
-
-
private async void FileDownloadAsync(string url)
-
-
-
await System.Threading.Tasks.Task.Run(async () =>
-
-
-
-
-
using (HttpClient http = new HttpClient())
-
-
var httpResponseMessage = await http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
-
var contentLength = httpResponseMessage.Content.Headers.ContentLength;
-
string contentType = httpResponseMessage.Content.Headers.ContentType.MediaType??MimeMapping.GetMimeMapping(url);
-
string fileName = httpResponseMessage.Content.Headers.ContentDisposition!=null?httpResponseMessage.Content.Headers.ContentDisposition.FileName:Path.GetFileName(url);
-
-
-
fileName = Path.GetFileName(url);
-
-
using (var stream = await httpResponseMessage.Content.ReadAsStreamAsync())
-
-
-
byte[] bytes = new byte[readLength];
-
-
var beginSecond = DateTime.Now.Second;
-
-
-
Response.ContentType = "application/octet-stream";
-
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
-
-
Response.AddHeader("Content-Length", contentLength.HasValue?contentLength.Value.ToString():"0");
-
while ((writeLength = stream.Read(bytes, 0, readLength)) > 0)
-
-
-
if (Response.IsClientConnected)
-
-
Response.OutputStream.Write(bytes, 0, writeLength);
-
-
-
-
downloadSize += writeLength;
-
downloadSpeed += writeLength;
-
-
-
-
-
-
该文章在 2022/7/5 8:36:11 编辑过