c# http 下载文件 例子

类:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Threading;

namespace httpTool
{
    class HttpHelper
    {
        const int bytebuff = 1024;
        const int ReadWriteTimeOut = 2 * 1000;//超时等待时间
        const int TimeOutWait = 5 * 1000;//超时等待时间
        const int MaxTryTime = 12;

        private double totalSize, curReadSize, speed;
        private int proc, remainTime;
        private int totalTime = 0;

        bool downLoadWorking = false;

        string StrFileName = "";
        string StrUrl = "";

        string outMsg = "";

        public HttpHelper(string url, string savePath)
        {
            this.StrUrl = url;
            this.StrFileName = savePath;
        }

        /// <summary>
        /// 下载数据更新
        /// </summary>
        /// <param name="totalNum">下载文件总大小</param>
        /// <param name="num">已下载文件大小</param>
        /// <param name="proc">下载进度百分比</param>
        /// <param name="speed">下载速度</param>
        /// <param name="remainTime">剩余下载时间</param>
        public delegate void delDownFileHandler(string totalNum, string num, int proc, string speed, string remainTime, string outMsg);
        public delDownFileHandler processShow;
        public delegate void delDownCompleted();
        public delDownCompleted processCompleted;
        public System.Windows.Forms.Timer timer;

        public void init()
        {
            timer.Interval = 100;
            timer.Tick -= TickEventHandler;
            timer.Tick += TickEventHandler;
            timer.Enabled = true;
            downLoadWorking = true;
        }

        /// <summary>
        /// 获取文件大小
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        private string GetSize(double size)
        {
            String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB" };
            double mod = 1024.0;
            int i = 0;
            while (size >= mod)
            {
                size /= mod;
                i++;
            }
            return Math.Round(size) + units[i];
        }

        /// <summary>
        /// 获取时间
        /// </summary>
        /// <param name="second"></param>
        /// <returns></returns>
        private string GetTime(int second)
        {
            return new DateTime(1970, 01, 01, 00, 00, 00).AddSeconds(second).ToString("HH:mm:ss");
        }

        /// <summary>
        /// 下载文件(同步)  支持断点续传
        /// </summary>
        public void DowLoadFile()
        {
            totalSize = GetFileContentLength(StrUrl);

            //打开上次下载的文件或新建文件
            long lStartPos = 0;
            System.IO.FileStream fs;
            if (System.IO.File.Exists(StrFileName))
            {
                fs = System.IO.File.OpenWrite(StrFileName);
                lStartPos = fs.Length;
                fs.Seek(lStartPos, System.IO.SeekOrigin.Current);   //移动文件流中的当前指针
            }
            else
            {
                fs = new System.IO.FileStream(StrFileName, System.IO.FileMode.Create);
                lStartPos = 0;
            }

            curReadSize = lStartPos;

            if (curReadSize == totalSize)
            {
                outMsg = "文件已下载!";
                processCompleted?.Invoke();
                timer.Enabled = false;
                return;
            }

            //打开网络连接
            try
            {
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(StrUrl);
                if (lStartPos > 0)
                    request.AddRange((int)lStartPos);    //设置Range值

                //向服务器请求,获得服务器回应数据流
                System.IO.Stream ns = request.GetResponse().GetResponseStream();
                byte[] nbytes = new byte[bytebuff];
                int nReadSize = 0;
                proc = 0;

                do
                {

                    nReadSize = ns.Read(nbytes, 0, bytebuff);
                    fs.Write(nbytes, 0, nReadSize);

                    //已下载大小
                    curReadSize += nReadSize;
                    //进度百分比
                    proc = (int)((curReadSize / totalSize) * 100);
                    //下载速度
                    speed = (curReadSize / totalTime) * 10;
                    //剩余时间
                    remainTime = (int)((totalSize / speed) - (totalTime / 10));

                    if (downLoadWorking == false)
                        break;

                } while (nReadSize > 0);

                fs.Close();
                ns.Close();

                if (curReadSize == totalSize)
                {
                    outMsg = "下载完成!";
                    processCompleted?.Invoke();
                    downLoadWorking = false;
                }
            }
            catch (Exception ex)
            {
                fs.Close();
                outMsg = string.Format("下载失败:{0}", ex.ToString());
            }
        }

        public void DownLoadPause()
        {
            outMsg = "下载已暂停";
            downLoadWorking = false;
        }

        public void DownLoadContinue()
        {
            outMsg = "正在下载";
            downLoadWorking = true;
            DownLoadStart();
        }

        public void DownLoadStart()
        {
            Task.Run(() =>
            {
                DowLoadFile();
            });
        }


        /// <summary>
        /// 定时器方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TickEventHandler(object sender, EventArgs e)
        {
            processShow?.Invoke(GetSize(totalSize),
                                GetSize(curReadSize),
                                proc,
                                string.Format("{0}/s", GetSize(speed)),
                                GetTime(remainTime),
                                outMsg
                                );
            if (downLoadWorking == true)
            {
                totalTime++;
            }
        }

        /// <summary>
        /// 获取下载文件长度
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public long GetFileContentLength(string url)
        {
            HttpWebRequest request = null;
            try
            {
                request = (HttpWebRequest)HttpWebRequest.Create(url);
                //request.Timeout = TimeOutWait;
                //request.ReadWriteTimeout = ReadWriteTimeOut;
                //向服务器请求,获得服务器回应数据流
                WebResponse respone = request.GetResponse();
                request.Abort();
                return respone.ContentLength;
            }
            catch (Exception e)
            {
                if (request != null)
                    request.Abort();
                return 0;
            }
        }
    }
}

页面调用:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using httpTool;
using System.Threading;

namespace DownLoadDemo1
{
    public partial class Form1 : Form
    {
        static string url = "http://speedtest.dallas.linode.com/100MB-dallas.bin"; //下载地址

        static string temp = System.Environment.GetEnvironmentVariable("TEMP");
        static string loadFilefolder = new System.IO.DirectoryInfo(temp).FullName + @"\" + "100MB-dallas.bin";

        Thread td;
        HttpHelper httpManager = new HttpHelper(url, loadFilefolder);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            httpManager.timer = new System.Windows.Forms.Timer();
            httpManager.processShow += processSho;
            httpManager.processCompleted += processComplete;
            httpManager.init();

        }

        public void processSho(string totalNum, string num, int proc, string speed, string remainTime, string msg)
        {
            this.label1.Text = string.Format("文件大小:{0}", totalNum);
            this.label2.Text = string.Format("已下载:{0}", num);
            this.label3.Text = string.Format("进度:{0}%", proc);
            this.label4.Text = msg;
            this.label5.Text = string.Format("速度:{0}",speed);
            this.label6.Text = string.Format("剩余时间:{0}",remainTime);
        }

        private void processComplete()
        {
            MessageBox.Show("文件下载完成!", "提示");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            httpManager.DownLoadStart();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            httpManager.DownLoadPause();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            httpManager.DownLoadContinue();
        }
    }
}

效果:

img

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值