Example #1
0
        public void DownloadFile(string url, string fileName)
        {
            HttpWebRequest request;
            HttpWebResponse response = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                response = (HttpWebResponse)request.GetResponse();
                string range = response.Headers["Content-Range"];
                List<int> ran = new List<int>();
                ran = GetRange(range);//求出要下的文件分块的起始点,终点,以及总长

                int len = ran[2];//分块长度
                int offset = ran[0];
                DownLoadState x = new DownLoadState(url, response, fileName, offset, len, new DownLoadState.ThreadCallbackHandler(ResponseAsBytes));
                //DownLoadState x = new DownLoadState(url,response,offset,len,new DownLoadState.ThreadCallbackHandler(ResponseAsBytes));
                //       单线程下载
                //       x.StartDownloadFileChunk();
                x.httpWebClient = this;
                //多线程下载

                _SmartThreadPool.QueueWorkItem(new Action(x.StartDownloadFileChunk));

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

            }
            catch (Exception e)
            {
                //ExceptionEventArgs.ExceptionActions ea = ExceptionEventArgs.ExceptionActions.Throw;
                //if (this.ExceptionOccurrs != null)
                //{
                //    DownLoadState x = new DownLoadState(url, response.ResponseUri.AbsolutePath, fileName, realFileName, offset, blockSize);
                //    ExceptionEventArgs eea = new ExceptionEventArgs(e, x);
                //    ExceptionOccurrs(this, eea);
                //    ea = eea.ExceptionAction;
                //}
                //if (ea == ExceptionEventArgs.ExceptionActions.Throw)
                //{
                //    if (!(e is WebException) && !(e is SecurityException))
                //    {
                //        throw new WebException("net_webclient", e);
                //    }
                //    throw;
                //}
            }
        }
Example #2
0
        internal byte[] ResponseAsBytes(string RequestURL, WebResponse Response, int Length, int ptr, string FileName)
        {
            // string a = null; //AttachmentName
            int filePtr = 0; //整个文件的位置指针
            int readBytes = 0;
            try
            {
                //a = Response.Headers["Content-Disposition"]; //attachment
                //if (a != null)
                //{
                //    a = a.Substring(a.LastIndexOf("filename=") + 9);
                //}
                long blockSize = Length; //Response.ContentLength;
                bool flagNoLen = false;
                if (blockSize == -1)//这里可能有问题
                {
                    flagNoLen = true;
                    blockSize = 0x10000; //64k
                }
                byte[] buffer1 = new byte[(int)blockSize];
                int blockPtr = 0; //本块的位置指针
                //string s = Response.Headers["Content-Range"];
                //if (s != null)
                //{
                //    s = s.Replace("bytes ", "");
                //    s = s.Substring(0, s.IndexOf("-"));
                //    filePtr = Convert.ToInt32(s);
                //}
                filePtr = ptr;
                int totalRead = 0;
                Stream responseStream = Response.GetResponseStream();
                do
                {
                    readBytes = responseStream.Read(buffer1, totalRead, ((int)blockSize) - totalRead);
                    totalRead += readBytes;
                    if (flagNoLen && (totalRead == blockSize))
                    {
                        blockSize += 0x10000;
                        byte[] buffer2 = new byte[(int)blockSize];
                        Buffer.BlockCopy(buffer1, 0, buffer2, 0, totalRead);
                        buffer1 = buffer2;
                    }
                    //    lock (_SyncLockObject)
                    //    {
                    //     this._bytes += num2;
                    //    }
                    if (readBytes > 0)
                    {
                        if (this.DataReceive != null)
                        {
                            byte[] buffer = new byte[readBytes];
                            Buffer.BlockCopy(buffer1, blockPtr, buffer, 0, buffer.Length);
                            //写硬盘
                            DownLoadState dls = new DownLoadState(FileName, filePtr, readBytes, buffer);
                            DownLoadEventArgs dlea = new DownLoadEventArgs(dls);
                            //触发事件
                            this.OnDataReceive(dlea);
                            //System.Threading.Thread.Sleep(100);
                        }
                        blockPtr += readBytes; //本块的位置指针
                        filePtr += readBytes; //整个文件的位置指针
                    }
                    else
                    {
                        break;
                    }
                }
                while (readBytes != 0);

                responseStream.Close();
                responseStream = null;
                if (flagNoLen)
                {
                    byte[] buffer3 = new byte[totalRead];
                    Buffer.BlockCopy(buffer1, 0, buffer3, 0, totalRead);
                    buffer1 = buffer3;
                }
                return buffer1;
            }
            catch (Exception e)
            {
                //ExceptionEventArgs.ExceptionActions ea = ExceptionEventArgs.ExceptionActions.Throw;
                //if (this.ExceptionOccurrs != null)
                //{
                //    DownLoadState x = new DownLoadState(RequestURL, Response.ResponseUri.AbsolutePath, FileName, a, filePtr, readBytes);
                //    ExceptionEventArgs eea = new ExceptionEventArgs(e, x);
                //    ExceptionOccurrs(this, eea);
                //    ea = eea.ExceptionAction;
                //}
                //if (ea == ExceptionEventArgs.ExceptionActions.Throw)
                //{
                //    if (!(e is WebException) && !(e is SecurityException))
                //    {
                //        throw new WebException("net_webclient", e);
                //    }
                //    throw;
                //}
                //return null;
                return null;
            }
        }
Example #3
0
 internal ExceptionEventArgs(System.Exception e, DownLoadState DownloadState)
 {
     this._Exception = e;
     this._DownloadState = DownloadState;
 }