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

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

问题描述

如何实现从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#从头web服务器中的两种方法之一下载:

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

[1]使用System.Net高层次的API,如HttpWebRequest的,HttpWebResponse,的FtpWebRequest,和其他类。

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

[2]使用的System.Net.Sockets如TcpClient的,的TcpListener和Socket类的底层API。

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

使用第一种方式的好处是,你通常不必担心低级别的管道,如preparing和跨preting HTTP标头和处理代理,认证,缓存等。高层次的类为你做这个,所以我preFER这种方法。

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.

使用第一种方法,一个典型的code至prepare一个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();

在上述code结束时,你会得到一个网络流对象,然后你可以用它来读取远程文件的字节数,如果你正在阅读的任何其他流对象。现在,无论是远程URL支持,允许你在任何arbitary位置读取由接受范围决定恢复部分下载 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.

其实,这code是一个更大的开源下载管理器,我想在C#中实施的一部分。您可以参考此应用程序,看看是否有什么可以对你有所帮助: HTTP://scavenger.$c$ cplex.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/

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

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