• c# Http下载


    1.首先是服务器上发布资源,如果资源时自定义格式,比如 .zidingyi结尾的后缀文件,需要在MIME类型上添加处理方式

    需要注意的是.net里面要设置

    System.Net.ServicePointManager.DefaultConnectionLimit = 50;

    2.客户端采用WebRequest进行请求,WebRequest处理响应   

    /// <summary>
    /// Http协议下载助手
    /// </summary>
    public class HttpDownLoadHelper
    {
    #region 变量,属性
    private HttpWebRequest request;
    private HttpWebResponse response;
    public string SavePath { get; set; }
    public string URL { get; set; }
    public string State { get; set; }
    private int iReConnectTimes = 0;//重连次数
    public long DownLoadedSize { get; set; }//已下载大小
    private bool IFAbortExec = true;//是否异常中断
    FileStream fs;
    Stream reader;
    #endregion

    #region 构造函数
    public HttpDownLoadHelper()
    {
    }

    public HttpDownLoadHelper(string vsServerFilePath, string vsSavePath)
    {
    //request = (HttpWebRequest)HttpWebRequest.Create(new Uri(vsServerFilePath));
    this.URL = vsServerFilePath;
    SavePath = vsSavePath;
    }
    #endregion

    #region 方法
    /// <summary>
    /// 读取已下载文件大小
    /// </summary>
    public void Init()
    {
    if (File.Exists(SavePath))//断点续传
    {
    fs = File.OpenWrite(SavePath);
    DownLoadedSize = fs.Length;
    fs.Close();
    }
    }

    public void DownLoadFile(string vsServerFilePath, string vsSavePath)
    {
    URL = vsServerFilePath;
    SavePath = vsSavePath;
    DownLoadFile();
    }

    public void DownLoadFile()
    {
    try
    {
    request = (HttpWebRequest)HttpWebRequest.Create(new Uri(URL));
    request.KeepAlive = true;
    request.Timeout = 1000 * 10;

    request.ReadWriteTimeout = 1000 * 20;//不知道为啥,流read的时候会超时
    long startPos = 0;
    if (File.Exists(SavePath))//断点续传
    {
    fs = File.OpenWrite(SavePath);
    if (fs.Length > 0)//对于已下载的索引文件,重新下载最后一个字节,否则AddRange会出错,因为文件大小获取是总的
    {
    fs.Seek(fs.Length - 1, SeekOrigin.Current);
    startPos = fs.Length - 1;
    }
    else
    {
    fs.Seek(fs.Length, SeekOrigin.Current);
    startPos = fs.Length;
    }
    }
    else//全新下载
    {
    string vsDir = Path.GetDirectoryName(SavePath);
    if (!Directory.Exists(vsDir))
    {
    Directory.CreateDirectory(vsDir);
    }
    fs = new FileStream(SavePath, FileMode.Create);
    startPos = 0;
    }
    DownLoadedSize = startPos;
    if (!string.IsNullOrEmpty(State) && State.ToUpper() == "PAUSED")
    {
    fs.Close();
    return;
    }
    if (startPos > 0)
    {
    request.AddRange(startPos);
    }
    byte[] buffer = new byte[1024 * 32];
    response = (HttpWebResponse)request.GetResponse();
    reader = response.GetResponseStream();
    int iRead = reader.Read(buffer, 0, buffer.Length);
    DownLoadedSize += iRead;
    while (iRead > 0)
    {
    fs.Write(buffer, 0, iRead);
    iRead = reader.Read(buffer, 0, buffer.Length);
    DownLoadedSize += iRead;
    }
    fs.Close();
    reader.Close();

    if (response != null)
    {
    response.Close();
    }
    if (request != null)
    {
    request.Abort();
    }

    }
    catch (Exception ex)
    {
    if (IFAbortExec)
    {
    Logger.LogError(ex);
    }
    //断线重连
    ++iReConnectTimes;
    if (iReConnectTimes > 5 || !IFAbortExec)//清理资源
    {
    fs.Close();
    if (reader != null)
    {
    reader.Close();
    }

    if (response != null)
    {
    response.Close();
    }
    if (request != null)
    {
    request.Abort();
    }
    }
    else if (IFAbortExec)//异常中断才可重连,先清理资源
    {

    fs.Close();
    if (reader != null)
    {
    reader.Close();
    }

    if (response != null)
    {
    response.Close();
    }
    if (request != null)
    {
    request.Abort();
    }
    DownLoadFile();
    }


    }
    }

    public void GetDownLoadedSize()
    {
    long startPos = 0;
    if (File.Exists(SavePath))//断点续传
    {
    fs = File.OpenWrite(SavePath);
    fs.Seek(fs.Length, SeekOrigin.Current);
    startPos = fs.Length;
    }
    else//全新下载
    {
    fs = new FileStream(SavePath, FileMode.Create);
    startPos = 0;
    }
    DownLoadedSize = startPos;
    }

    public void Abort()
    {
    try
    {
    IFAbortExec = false;//非异常中断
    if (request != null)
    {
    request.Abort();
    }
    if (response != null)
    {
    response.Close();
    }
    if (fs != null)
    {
    fs.Close();
    }
    if (reader != null)
    {
    reader.Close();
    }

    }
    catch (Exception ex)
    {
    Logger.LogError(ex);
    }
    }

    public void Cancel()
    {

    Abort();
    //删除文件
    try
    {
    if (File.Exists(SavePath))
    {
    File.Delete(SavePath);
    }
    }
    catch (Exception ex)
    {
    Logger.LogError(ex);
    }

    }
    #endregion
    }

  • 相关阅读:
    令人惊艳的算法——蒙特卡洛采样法
    肺炎确诊人数增长趋势拟合和预测(截止1月28日)
    「07」回归的诱惑:深入浅出逻辑回归
    实现一个简易版Webpack
    nrm——快速切换npm源
    7个有用的JavaScript技巧
    JSON Web Token 是什么?
    【译】强化表单的9个Vue输入库
    【译】如何使用Vue过渡效果来提升用户体验
    【译】JavaScript Source Maps浅析
  • 原文地址:https://www.cnblogs.com/niuge/p/3614148.html
Copyright © 2020-2023  润新知