使用 .NET 中的恢复/重试支持从 HTTP 下载大文件? [英] Download large file from HTTP with resume/retry support in .NET?

查看:18
本文介绍了使用 .NET 中的恢复/重试支持从 HTTP 下载大文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

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

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

推荐答案

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

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

  1. 使用 System.Net 中的高级 API,例如 HttpWebRequestHttpWebResponseFtpWebRequest 和其他类.

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

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

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

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 请求以从 url 下载文件的典型代码如下所示:

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

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();        

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

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.

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

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/

这篇关于使用 .NET 中的恢复/重试支持从 HTTP 下载大文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆