private void x_DataReceive(HttpWebClient Sender, DownLoadEventArgs e) { if (!this.b) { lock (_SyncLockObject) { if (!this.b) { System.Console.Write(System.DateTime.Now.ToString() + " 已接收数据: "); //System.Console.Write( System.DateTime.Now.ToString() + " 已接收数据: "); this.b = true; } } } string f = e.DownloadState.FileName; if (e.DownloadState.AttachmentName != null) { f = System.IO.Path.GetDirectoryName(f) + @"\" + e.DownloadState.AttachmentName; } this._f = f; using (System.IO.FileStream sw = new System.IO.FileStream(f, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite)) { sw.Position = e.DownloadState.Position; sw.Write(e.DownloadState.Data, 0, e.DownloadState.Data.Length); sw.Close(); } string s = System.DateTime.Now.ToString(); lock (_SyncLockObject) { this.i += e.DownloadState.Data.Length; System.Console.Write(bs + "\b\b\b\b\b\b\b\b\b\b" + i + " / " + Sender.FileLength + " 字节数据 " + s); //System.Console.Write(bs + i + " 字节数据 " + s); this.bs = new string('\b', Digits(i) + 3 + Digits(Sender.FileLength) + s.Length); } }
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; //整个文件的位置指针 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); } }
private void x_DataReceive(HttpWebClient Sender, DownLoadEventArgs e) { if (!this.b) { lock (_SyncLockObject) { if (!this.b) { System.Console.Write(System.DateTime.Now.ToString() + " 已接收数据: "); //System.Console.Write( System.DateTime.Now.ToString() + " 已接收数据: "); this.b = true; } } } string f = e.DownloadState.FileName; if (e.DownloadState.AttachmentName != null) f = System.IO.Path.GetDirectoryName(f) + @"\" + e.DownloadState.AttachmentName; this._f = f; using (System.IO.FileStream sw = new System.IO.FileStream(f, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite)) { sw.Position = e.DownloadState.Position; sw.Write(e.DownloadState.Data, 0, e.DownloadState.Data.Length); sw.Close(); } string s = System.DateTime.Now.ToString(); lock (_SyncLockObject) { this.i += e.DownloadState.Data.Length; System.Console.Write(bs + "\b\b\b\b\b\b\b\b\b\b" + i + " / " + Sender.FileLength + " 字节数据 " + s); //System.Console.Write(bs + i + " 字节数据 " + s); this.bs = new string('\b', Digits(i) + 3 + Digits(Sender.FileLength) + s.Length); } }
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; } }