internal byte[] ResponseAsBytes(string RequestURL, WebResponse Response, long Length, string FileName) { string a = null; //AttachmentName int P = 0; //整个文件的位置指针 int num2 = 0; try { a = Response.Headers["Content-Disposition"]; //attachment if (a != null) { a = a.Substring(a.LastIndexOf("filename=") + 9); } long num1 = Length; //Response.ContentLength; bool flag1 = false; if (num1 == -1) { flag1 = true; num1 = 0x10000; //64k } byte[] buffer1 = new byte[(int)num1]; int p = 0; //本块的位置指针 string s = Response.Headers["Content-Range"]; if (s != null) { s = s.Replace("bytes ", ""); s = s.Substring(0, s.IndexOf("-")); P = Convert.ToInt32(s); } int num3 = 0; Stream S = Response.GetResponseStream(); do { num2 = S.Read(buffer1, num3, ((int)num1) - num3); num3 += num2; if (flag1 && (num3 == num1)) { num1 += 0x10000; byte[] buffer2 = new byte[(int)num1]; Buffer.BlockCopy(buffer1, 0, buffer2, 0, num3); buffer1 = buffer2; } // lock (_SyncLockObject) // { // this._bytes += num2; // } if (num2 > 0) { if (this.DataReceive != null) { byte[] buffer = new byte[num2]; Buffer.BlockCopy(buffer1, p, buffer, 0, buffer.Length); DownLoadState dls = new DownLoadState(RequestURL, Response.ResponseUri.AbsolutePath, FileName, a, P, num2, buffer); DownLoadEventArgs dlea = new DownLoadEventArgs(dls); //触发事件 this.OnDataReceive(dlea); //System.Threading.Thread.Sleep(100); } p += num2; //本块的位置指针 P += num2; //整个文件的位置指针 } else { break; } }while (num2 != 0); S.Close(); S = null; if (flag1) { byte[] buffer3 = new byte[num3]; Buffer.BlockCopy(buffer1, 0, buffer3, 0, num3); buffer1 = buffer3; } return(buffer1); } catch (Exception e) { ExceptionActions ea = ExceptionActions.Throw; if (this.ExceptionOccurrs != null) { DownLoadState x = new DownLoadState(RequestURL, Response.ResponseUri.AbsolutePath, FileName, a, P, num2); ExceptionEventArgs eea = new ExceptionEventArgs(e, x); ExceptionOccurrs(this, eea); ea = eea.ExceptionAction; } if (ea == ExceptionActions.Throw) { if (!(e is WebException) && !(e is SecurityException)) { throw new WebException("net_webclient", e); } throw; } return(null); } }
/// <summary> /// 分块下载文件 /// </summary> /// <param name="Address">URL 地址</param> /// <param name="FileName">保存到本地的路径文件名</param> /// <param name="ChunksCount">块数,线程数</param> public void DownloadFile(string Address, string FileName, int ChunksCount) { int p = 0; // position int s = 0; // chunk size string a = null; HttpWebRequest hwrq; HttpWebResponse hwrp = null; try { hwrq = (HttpWebRequest)WebRequest.Create(this.GetUri(Address)); hwrp = (HttpWebResponse)hwrq.GetResponse(); long L = hwrp.ContentLength; hwrq.Credentials = this.m_credentials; L = ((L == -1) || (L > 0x7fffffff)) ? ((long)0x7fffffff) : L; //Int32.MaxValue 该常数的值为 2,147,483,647; 即十六进制的 0x7FFFFFFF int l = (int)L; this._FileLength = l; // 在本地预定空间(竟然在多线程下不用先预定空间) // FileStream sw = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); // sw.Write(new byte[l], 0, l); // sw.Close(); // sw = null; bool b = (hwrp.Headers["Accept-Ranges"] != null & hwrp.Headers["Accept-Ranges"] == "bytes"); a = hwrp.Headers["Content-Disposition"]; //attachment if (a != null) { a = a.Substring(a.LastIndexOf("filename=") + 9); } else { a = FileName; } int ss = s; if (b) { s = l / ChunksCount; if (s < 2 * 64 * 1024) //块大小至少为 128 K 字节 { s = 2 * 64 * 1024; } ss = s; int i = 0; while (l > s) { l -= s; if (l < s) { s += l; } if (i++ > 0) { DownLoadState x = new DownLoadState(Address, hwrp.ResponseUri.AbsolutePath, FileName, a, p, s, new ThreadCallbackHandler(this.DownloadFileChunk)); // 单线程下载 // x.StartDownloadFileChunk(); x.httpWebClient = this; //多线程下载 Thread t = new Thread(new ThreadStart(x.StartDownloadFileChunk)); //this.OnThreadProcess(t); t.Start(); } p += s; } s = ss; byte[] buffer = this.ResponseAsBytes(Address, hwrp, s, FileName); this.OnThreadProcess(Thread.CurrentThread); // lock (_SyncLockObject) // { // this._Bytes += buffer.Length; // } } } catch (Exception e) { ExceptionActions ea = ExceptionActions.Throw; if (this.ExceptionOccurrs != null) { DownLoadState x = new DownLoadState(Address, hwrp.ResponseUri.AbsolutePath, FileName, a, p, s); ExceptionEventArgs eea = new ExceptionEventArgs(e, x); ExceptionOccurrs(this, eea); ea = eea.ExceptionAction; } if (ea == ExceptionActions.Throw) { if (!(e is WebException) && !(e is SecurityException)) { throw new WebException("net_webclient", e); } throw; } } }