日期:2014-05-17  浏览次数:20863 次

C#实现文件下载,支持断点续传
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.IO;
using System.Text;
using System.Net;
namespace simpleDemo
{
    
    class Program
    {
        /// <summary>
        /// 下载文件保留字
        /// </summary>
        public static string PERSIST_EXP = ".cdel";
        /// <summary>  
        public static void Main(string[] args)
        {

            string path = "D:\\aa.txt";

            string ec = getFileEncoding(path, "GB2312");
            print("coding: " + ec);

            // string content = fileReader(path, Encoding.GetEncoding(ec));
            // print(content);

            //fileWriter(path, "测试内容11", Encoding.GetEncoding(ec));

            string url = "http://www.XXX.com/20120920172200024.flv";
            string path1 = "D:\\aa1.flv";


            download(url, path1);
            //gapDownload(url, path1);
            //t(url);
           
        }
        public static void t(string url) {
            
            HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create(url);


            //WebResponse response = httpClient.CreateGetHttpResponse(url, 3000, null, null);

            try {
                
                WebResponse response = request.GetResponse();

                WebHeaderCollection headers = response.Headers;

                print(response.ContentLength);

                request = (System.Net.HttpWebRequest)HttpWebRequest.Create(url);
                request.AddRange(11); //设置Range值
                WebResponse response1 = request.GetResponse();
                print(response1.ContentLength);


                foreach (string key in headers)