static void Main() { AppTest a = new AppTest(); HttpWebClient x = new HttpWebClient(); a._K = 10; //订阅 DataReceive 事件 x.DataReceive += new HttpWebClient.DataReceiveEventHandler(a.x_DataReceive); //订阅 ExceptionOccurrs 事件 x.ExceptionOccurrs += new HttpWebClient.ExceptionEventHandler(a.x_ExceptionOccurrs); x.ThreadProcessEnd += new HttpWebClient.ThreadProcessEventHandler(a.x_ThreadProcessEnd); string F = "http://localhost/download/phpMyAdmin-2.6.1-pl2.zip"; a._F = F; F = "http://localhost/download/jdk-1_5_0_01-windows-i586-p.aa.exe"; //F = "http://localhost/download/ReSharper1.5.exe"; //F = "http://localhost/mywebapplications/WebApplication7/WebForm1.aspx"; //F = "http://*****:*****@"E:\temp\" + f, 10, x).StartThreadProcess))).Start(); x.DownloadFile(F, @"E:\temp\temp\" + f, a._K); // x.DownloadFileChunk(F, @"E:\temp\" + f,15,34556); System.Console.ReadLine(); // string uploadfile = "e:\\test_local.rar"; // string str = x.UploadFileEx("http://localhost/phpmyadmin/uploadaction.php", "POST", uploadfile, "file1"); // System.Console.WriteLine(str); // System.Console.ReadLine(); }
private void x_ThreadProcessEnd(HttpWebClient Sender, ThreadProcessEventArgs e) { //if (e.thread.ThreadState == System.Threading.ThreadState.Stopped) if (this._k++ == this._K - 1) { System.Console.WriteLine("\nend"); } }
private void x_ExceptionOccurrs(HttpWebClient Sender, ExceptionEventArgs e) { System.Console.WriteLine(e.Exception.Message); //发生异常重新下载相当于断点续传,你可以自己自行选择处理方式 HttpWebClient x = new HttpWebClient(); x.DownloadFileChunk(this._F, this._f, e.DownloadState.Position, e.DownloadState.Length); e.ExceptionAction = ExceptionActions.Ignore; }
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 x_ThreadProcessEnd(HttpWebClient Sender, ThreadProcessEventArgs e) { //if (e.thread.ThreadState == System.Threading.ThreadState.Stopped) if (this._k++ == this._K - 1) System.Console.WriteLine("\nend"); }
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); } }