• Http 下载文件 并显示进度条


    /// <summary>
    /// HTTP 下载文件
    /// Current Thread Download
    /// </summary>
    /// <param name="URL">下载文件地址</param>
    /// <param name="buffer">将下载的文件存入该字节数组</param>
    /// <param name="Prog">用于显示的进度条</param>
    public void DownloadFile(string URL, ref byte[] buffer, ProgressBar prog)
    {
    try
    {
    System.Net.HttpWebRequest Myrq
    = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
    System.Net.HttpWebResponse myrp
    = (System.Net.HttpWebResponse)Myrq.GetResponse();
    long totalBytes = myrp.ContentLength;
    if (prog != null)
    {
    prog.Maximum
    = (int)totalBytes;
    }
    using (MemoryStream ms = new MemoryStream((int)totalBytes))
    {
    Stream st
    = myrp.GetResponseStream();
    long totalDownloadedByte = 0;
    byte[] by = new byte[1024];
    int osize = st.Read(by, 0, (int)by.Length);
    while (osize > 0)
    {
    totalDownloadedByte
    = osize + totalDownloadedByte;
    System.Windows.Forms.Application.DoEvents();
    ms.Write(by,
    0, osize);
    if (prog != null)
    {
    prog.Value
    = (int)totalDownloadedByte;
    }
    osize
    = st.Read(by, 0, (int)by.Length);
    }
    buffer
    = ms.ToArray();
    st.Close();
    }
    }
    catch (Exception ex)
    {
    buffer
    = null;
    Console.WriteLine(ex.Message);
    }
    }

    嘻、 request 和 response 都比较好用。


    返回导读目录,阅读更多随笔



    分割线,以下为博客签名:

    软件臭虫情未了
    • 编码一分钟
    • 测试十年功


    随笔如有错误或不恰当之处、为希望不误导他人,望大侠们给予批评指正。

  • 相关阅读:
    STDMETHOD (转)
    DirectX中的纹理映射相关技术 (转)
    (转)清空std::stringstream,联系到stream的clear()和清空
    (转载)MultiAnimation
    (转)SkyBox
    [转载]漫谈游戏中的阴影技术
    反射矩阵计算
    (转)COM组件里的AddRef()
    LINQ简记(2):重要概念
    继续聊WPF——自定义命令
  • 原文地址:https://www.cnblogs.com/08shiyan/p/1972416.html
Copyright © 2020-2023  润新知