http分段下载

class Downloader
    {
        /// <summary>
        /// 下载一个文件块,利用该方法可自行实现多线程断点续传
        /// </summary>
        /// <param name="Address">URL 地址</param>
        /// <param name="FileName">保存到本地的路径文件名</param>
        /// <param name="Length">块大小</param>
        public void DownloadFileChunk(string Address, string FileName, int FromPosition, int Length)
        {
            HttpWebResponse hwrp = null;
            string a = null;
            try
            {
                //this._FileName = FileName;
                HttpWebRequest hwrq = (HttpWebRequest)WebRequest.Create(Address);
                //hwrq.Credentials = this.m_credentials;
                hwrq.AddRange(FromPosition);
                hwrp = (HttpWebResponse)hwrq.GetResponse();
                a = hwrp.Headers["Content-Disposition"]; //attachment
                if (a != null)
                {
                    a = a.Substring(a.LastIndexOf("filename=") + 9);
                }
                else
                {
                    a = FileName;
                }


                byte[] buffer = this.ResponseAsBytes(Address, hwrp, Length, FileName);

            }
            catch (Exception e)
            {
                //add you code.
            }
        }


        internal byte[] ResponseAsBytes(string RequestURL, WebResponse Response, long Length, string FileName)
        {

            byte[] buffers = new byte[Length];
            string s = Response.Headers["Content-Range"];
            //add your code.
            Stream S = Response.GetResponseStream();
            //add your code.
            S.Close();
            Response.Close();

            return buffers;

        }


    }

posted on 2011-03-19 23:57  ATAK  阅读(4360)  评论(0编辑  收藏  举报

导航