private void OnDataReceive(DownLoadEventArgs e) { //触发数据到达事件 DataReceive(this, e); }
private void OnDataReceive(DownLoadEventArgs e) { //触发数据到达事件 DataReceive(this, e); }
internal byte[] ResponseAsBytes(string RequestURL, WebResponse Response, long Length, string FileName) { string a = null; //AttachmentName int P = 0; //整个文件的位置指针 long num = 0; try { a = Response.Headers["Content-Disposition"]; //attachment if (a != null) { a = a.Substring(a.LastIndexOf("filename=") + 9); } num = Length; //Response.ContentLength; bool flag = false; if (num == -1) { flag = true; num = 0x0000; //6k } byte[] buffer = new byte[(int)num]; int p = 0; //本块的位置指针 string s = Response.Headers["Content-Range"]; if (s != null) { s = s.Replace("bytes ", ""); s = s.Substring(0, s.IndexOf("-")); P = Convert.Toint(s); } num = 0; Stream S = Response.GetResponseStream(); do { num = S.Read(buffer, num, ((int)num) - num); num += num; if (flag && (num == num)) { num += 0x0000; byte[] buffer = new byte[(int)num]; Buffer.BlockCopy(buffer, 0, buffer, 0, num); buffer = buffer; } // lock (_SyncLockObject) // { // this._bytes += num; // } if (num > 0) { if (this.DataReceive != null) { byte[] buffer = new byte[num]; Buffer.BlockCopy(buffer, p, buffer, 0, buffer.Length); DownLoadState dls = new DownLoadState(RequestURL, Response.ResponseUri.AbsolutePath, FileName, a, P, num, buffer); DownLoadEventArgs dlea = new DownLoadEventArgs(dls); //触发事件 this.OnDataReceive(dlea); //System.Threading.Thread.Sleep(00); } p += num; //本块的位置指针 P += num; //整个文件的位置指针 } else { break; } }while (num != 0); S.Close(); S = null; if (flag) { byte[] buffer = new byte[num]; Buffer.BlockCopy(buffer, 0, buffer, 0, num); buffer = buffer; } return(buffer); } catch (Exception e) { ExceptionActions ea = ExceptionActions.Throw; if (this.ExceptionOccurrs != null) { DownLoadState x = new DownLoadState(RequestURL, Response.ResponseUri.AbsolutePath, FileName, a, P, num); 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); } }
internal byte[] ResponseAsBytes(string RequestURL, WebResponse Response, long Length, string FileName) { string a = null; //AttachmentName int P = 0; //整个文件的位置指针 long num = 0; try { a = Response.Headers["Content-Disposition"]; //attachment if (a != null) { a = a.Substring(a.LastIndexOf("filename=") + 9); } num = Length; //Response.ContentLength; bool flag = false; if (num == -1) { flag = true; num = 0x0000; //6k } byte[] buffer = new byte[(int)num]; int p = 0; //本块的位置指针 string s = Response.Headers["Content-Range"]; if (s != null) { s = s.Replace("bytes ", ""); s = s.Substring(0, s.IndexOf("-")); P = Convert.Toint(s); } num = 0; Stream S = Response.GetResponseStream(); do { num = S.Read(buffer, num, ((int)num) - num); num += num; if (flag && (num == num)) { num += 0x0000; byte[] buffer = new byte[(int)num]; Buffer.BlockCopy(buffer, 0, buffer, 0, num); buffer = buffer; } // lock (_SyncLockObject) // { // this._bytes += num; // } if (num > 0) { if (this.DataReceive != null) { byte[] buffer = new byte[num]; Buffer.BlockCopy(buffer, p, buffer, 0, buffer.Length); DownLoadState dls = new DownLoadState(RequestURL, Response.ResponseUri.AbsolutePath, FileName, a, P, num, buffer); DownLoadEventArgs dlea = new DownLoadEventArgs(dls); //触发事件 this.OnDataReceive(dlea); //System.Threading.Thread.Sleep(00); } p += num; //本块的位置指针 P += num; //整个文件的位置指针 } else { break; } } while (num != 0); S.Close(); S = null; if (flag) { byte[] buffer = new byte[num]; Buffer.BlockCopy(buffer, 0, buffer, 0, num); buffer = buffer; } return buffer; } catch (Exception e) { ExceptionActions ea = ExceptionActions.Throw; if (this.ExceptionOccurrs != null) { DownLoadState x = new DownLoadState(RequestURL, Response.ResponseUri.AbsolutePath, FileName, a, P, num); 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; } }