C# 使用 .NET 中的恢复/重试支持从 HTTP 下载大文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16270216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:35:52  来源:igfitidea点击:

Download large file from HTTP with resume/retry support in .NET?

c#.nethttpfile-ioweb

提问by Louis Rhys

How to implement downloading a large file (~500MB) from HTTP in my application? I want to support automatic resume/retry, so that when connection is disconnected, my application can try to reconnect to get the file and avoid re-downloading the downloaded part, if possible (I know, this depends on the server as well).

如何在我的应用程序中实现从 HTTP 下载大文件(~500MB)?我想支持自动恢复/重试,这样当连接断开时,我的应用程序可以尝试重新连接以获取文件并避免重新下载下载的部分,如果可能的话(我知道,这也取决于服务器)。

This is similar to the behaviour in download managers and some browsers.

这类似于下载管理器和某些浏览器中的行为。

回答by lexeRoy

There's a Internet Download Managersupport for you to handle this. Download IDMCOMAPI.zip, Then with (Tlbimp) Type Library Importer, import the file IDManTypeInfo.tlbfrom the extracted zip.

有一个Internet 下载管理器支持可以处理这个问题。下载IDMCOMAPI.zip,然后使用 ( Tlbimp) 类型库导入器,IDManTypeInfo.tlb从解压缩的 zip 中导入文件。

cmd:

指令

C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\TlbExp.exe (FilePathGoesHere)\IDManTypeInfo.tlb

Take note:

做记录:

1.) You should run the Command Promptin an Administrator mode.

1.) 您应该Command Prompt以管理员模式运行。

2.) There are some files without resume capability.

2.) 有些文件没有恢复功能。

回答by Prahlad Yeri

You can implement downloading from a web-server in C#from scratch in one of the two ways:

您可以C#通过以下两种方式之一从头开始实现从网络服务器下载:

  1. Using the high-level APIs in System.Net such as HttpWebRequest, HttpWebResponse, FtpWebRequest, and other classes.

  2. Using the low-level APIs in System.Net.Sockets such as TcpClient, TcpListenerand Socket classes.

  1. 在System.Net使用高层次的API,例如HttpWebRequestHttpWebResponseFtpWebRequest,和其他类。

  2. 使用 System.Net.Sockets 中的低级 API,例如TcpClientTcpListener和 Socket 类。

The advantage of using the first approach is that you typically don't have to worry about the low level plumbing such as preparing and interpreting HTTP headers and handling the proxies, authentication, caching etc. The high-level classes do this for you and hence I prefer this approach.

使用第一种方法的优点是您通常不必担心低级管道,例如准备和解释 HTTP 标头以及处理代理、身份验证、缓存等。高级类为您完成这些工作,因此我更喜欢这种方法。

Using the first method, a typical code to prepare an HTTP request to download a file from a url will look something like this:

使用第一种方法,准备 HTTP 请求以从 url 下载文件的典型代码如下所示:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
if (UseProxy)
{
    request.Proxy = new WebProxy(ProxyServer + ":" + ProxyPort.ToString());
    if (ProxyUsername.Length > 0)
        request.Proxy.Credentials = new NetworkCredential(ProxyUsername, ProxyPassword);
}
//HttpWebRequest hrequest = (HttpWebRequest)request;
//hrequest.AddRange(BytesRead); ::TODO: Work on this
if (BytesRead > 0) request.AddRange(BytesRead);

WebResponse response = request.GetResponse();
//result.MimeType = res.ContentType;
//result.LastModified = response.LastModified;
if (!resuming)//(Size == 0)
{
    //resuming = false;
    Size = (int)response.ContentLength;
    SizeInKB = (int)Size / 1024;
}
acceptRanges = String.Compare(response.Headers["Accept-Ranges"], "bytes", true) == 0;

//create network stream
ns = response.GetResponseStream();        

At the end of the above code, you get a network-stream object which you can then use to read the bytes of the remote file as if you are reading any other stream object. Now, whether the remote url supports resuming partial downloads by allowing you to read from any arbitary position is determined by the "Accept-Ranges"HTTP header as shown above. If this value is set to anything other than "bytes", then this feature won't be supported.

在上述代码的末尾,您将获得一个网络流对象,然后您可以使用它来读取远程文件的字节,就像您正在读取任何其他流对象一样。现在,远程 url 是否支持通过允许您从任意位置读取来恢复部分下载由“Accept-Ranges”HTTP 标头决定,如上所示。如果此值设置为“字节”以外的任何值,则将不支持此功能。

In fact, this code is part of a bigger opensource download-manager that I'm trying to implement in C#. You may refer to this application and see if anything can be helpful to you: http://scavenger.codeplex.com/

事实上,这段代码是我试图用 C# 实现的一个更大的开源下载管理器的一部分。你可以参考这个应用程序,看看是否有什么对你有帮助的:http: //scavenger.codeplex.com/

回答by leonid p

There is open source .Net http file downloader with automatic resume/retry support (if server supports it), looks like it's exactly what you need, can try to use it:

有开源 .Net http 文件下载器,具有自动恢复/重试支持(如果服务器支持),看起来正是您所需要的,可以尝试使用它:

https://github.com/Avira/.NetFileDownloader.

https://github.com/Avira/.NetFileDownloader