C實現(xiàn)支持?jǐn)帱c續(xù)傳多線程下載客戶端工具類_第1頁
C實現(xiàn)支持?jǐn)帱c續(xù)傳多線程下載客戶端工具類_第2頁
C實現(xiàn)支持?jǐn)帱c續(xù)傳多線程下載客戶端工具類_第3頁
C實現(xiàn)支持?jǐn)帱c續(xù)傳多線程下載客戶端工具類_第4頁
C實現(xiàn)支持?jǐn)帱c續(xù)傳多線程下載客戶端工具類_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、C#實現(xiàn)支持?jǐn)帱c續(xù)傳多線程下載客戶端工具類C#實現(xiàn)支持?jǐn)帱c續(xù)傳多線程下載的 Http Web 客戶端工具類 (C# DIY HttpWebClient),感興趣的朋友可以參考下本文,或許對你有所幫助代碼如下:/* .Net/C#: 實現(xiàn)支持?jǐn)帱c續(xù)傳多線程下載的 Http Web 客戶端工具類 (C# DIY HttpWebClient) * Reflector 了一下 System.Net.WebClient ,改寫或增加了若干: * DownLoad、Upload 相關(guān)方法! * DownLoad 相關(guān)改動較大! * 增加了 DataReceive、ExceptionOccurrs 事件!

2、* 了解服務(wù)器端與客戶端交互的 HTTP 協(xié)議參閱: * 使文件下載的自定義連接支持 FlashGet 的斷點續(xù)傳多線程鏈接下載! JSP/Servlet 實現(xiàn)! * * 使文件下載的自定義連接支持 FlashGet 的斷點續(xù)傳多線程鏈接下載! C#/ASP.Net 實現(xiàn)! * */ /2005-03-14 修訂: /* .Net/C#: 實現(xiàn)支持?jǐn)帱c續(xù)傳多線程下載的工具類 * Reflector 了一下 System.Net.WebClient ,改寫或增加了若干: * DownLoad、Upload 相關(guān)方法! * 增加了 DataReceive、ExceptionOccurrs事件 */

3、 namespace Microshaoft.Utils using System; using System.IO; using System.Net; using System.Text; using System.Security; using System.Threading; using System.Collections.Specialized; / <summary> / 記錄下載的字節(jié)位置 / </summary> public class DownLoadState private string _FileName; private string _

4、AttachmentName; private int _Position; private string _RequestURL; private string _ResponseURL; private int _Length; private byte _Data; public string FileName get return _FileName; public int Position get return _Position; public int Length get return _Length; public string AttachmentName get retur

5、n _AttachmentName; public string RequestURL get return _RequestURL; public string ResponseURL get return _ResponseURL; public byte Data get return _Data; internal DownLoadState(string RequestURL, string ResponseURL, string FileName, string AttachmentName, int Position, int Length, byte Data) this._F

6、ileName = FileName; this._RequestURL = RequestURL; this._ResponseURL = ResponseURL; this._AttachmentName = AttachmentName; this._Position = Position; this._Data = Data; this._Length = Length; internal DownLoadState(string RequestURL, string ResponseURL, string FileName, string AttachmentName, int Po

7、sition, int Length, ThreadCallbackHandler tch) this._RequestURL = RequestURL; this._ResponseURL = ResponseURL; this._FileName = FileName; this._AttachmentName = AttachmentName; this._Position = Position; this._Length = Length; this._ThreadCallback = tch; internal DownLoadState(string RequestURL, str

8、ing ResponseURL, string FileName, string AttachmentName, int Position, int Length) this._RequestURL = RequestURL; this._ResponseURL = ResponseURL; this._FileName = FileName; this._AttachmentName = AttachmentName; this._Position = Position; this._Length = Length; private ThreadCallbackHandler _Thread

9、Callback; public HttpWebClient httpWebClient get return this._hwc; set this._hwc = value; internal Thread thread get return _thread; set _thread = value; private HttpWebClient _hwc; private Thread _thread; / internal void StartDownloadFileChunk() if (this._ThreadCallback != null) this._ThreadCallbac

10、k(this._RequestURL, this._FileName, this._Position, this._Length); this._hwc.OnThreadProcess(this._thread); /委托代理線程的所執(zhí)行的方法簽名一致 public delegate void ThreadCallbackHandler(string S, string s, int I, int i); /異常處理動作 public enum ExceptionActions Throw, CancelAll, Ignore, Retry / <summary> / 包含 Exc

11、eption 事件數(shù)據(jù)的類 / </summary> public class ExceptionEventArgs : System.EventArgs private System.Exception _Exception; private ExceptionActions _ExceptionAction; private DownLoadState _DownloadState; public DownLoadState DownloadState get return _DownloadState; public Exception Exception get retur

12、n _Exception; public ExceptionActions ExceptionAction get return _ExceptionAction; set _ExceptionAction = value; internal ExceptionEventArgs(System.Exception e, DownLoadState DownloadState) this._Exception = e; this._DownloadState = DownloadState; / <summary> / 包含 DownLoad 事件數(shù)據(jù)的類 / </summar

13、y> public class DownLoadEventArgs : System.EventArgs private DownLoadState _DownloadState; public DownLoadState DownloadState get return _DownloadState; public DownLoadEventArgs(DownLoadState DownloadState) this._DownloadState = DownloadState; public class ThreadProcessEventArgs : System.EventArg

14、s private Thread _thread; public Thread thread get return this._thread; public ThreadProcessEventArgs(Thread thread) this._thread = thread; / <summary> / 支持?jǐn)帱c續(xù)傳多線程下載的類 / </summary> public class HttpWebClient private static object _SyncLockObject = new object(); public delegate void DataR

15、eceiveEventHandler(HttpWebClient Sender, DownLoadEventArgs e); public event DataReceiveEventHandler DataReceive; /接收字節(jié)數(shù)據(jù)事件 public delegate void ExceptionEventHandler(HttpWebClient Sender, ExceptionEventArgs e); public event ExceptionEventHandler ExceptionOccurrs; /發(fā)生異常事件 public delegate void ThreadP

16、rocessEventHandler(HttpWebClient Sender, ThreadProcessEventArgs e); public event ThreadProcessEventHandler ThreadProcessEnd; /發(fā)生多線程處理完畢事件 private int _FileLength; /下載文件的總大小 public int FileLength get return _FileLength; / <summary> / 分塊下載文件 / </summary> / <param name="Address"

17、;>URL 地址</param> / <param name="FileName">保存到本地的路徑文件名</param> / <param name="ChunksCount">塊數(shù),線程數(shù)</param> public void DownloadFile(string Address, string FileName, int ChunksCount) int p = 0; / position int s = 0; / chunk size string a = null; Http

18、WebRequest 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.MaxVa

19、lue 該常數(shù)的值為 2,147,483,647; 即十六進制的 0x7FFFFFFF int l = (int) L; this._FileLength = l; / 在本地預(yù)定空間(竟然在多線程下不用先預(yù)定空間) / FileStream sw = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); / sw.Write(new bytel, 0, l); / sw.Close(); / sw = null; bool b = (hwrp.Headers&qu

20、ot;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 <

21、 2 * 64 * 1024) /塊大小至少為 128 K 字節(jié) 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.StartDown

22、loadFileChunk(); 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) /

23、 / 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); ExceptionOccu

24、rrs(this, eea); ea = eea.ExceptionAction; if (ea = ExceptionActions.Throw) if (!(e is WebException) && !(e is SecurityException) throw new WebException("net_webclient", e); throw; internal void OnThreadProcess(Thread t) if (ThreadProcessEnd != null) ThreadProcessEventArgs tpea = ne

25、w ThreadProcessEventArgs(t); ThreadProcessEnd(this, tpea); / <summary> / 下載一個文件塊,利用該方法可自行實現(xiàn)多線程斷點續(xù)傳 / </summary> / <param name="Address">URL 地址</param> / <param name="FileName">保存到本地的路徑文件名</param> / <param name="Length">塊大小</p

26、aram> public void DownloadFileChunk(string Address, string FileName, int FromPosition, int Length) HttpWebResponse hwrp = null; string a = null; try /this._FileName = FileName; HttpWebRequest hwrq = (HttpWebRequest) WebRequest.Create(this.GetUri(Address); /hwrq.Credentials = this.m_credentials; h

27、wrq.AddRange(FPosition); hwrp = (HttpWebResponse) hwrq.GetResponse(); a = hwrp.Headers"Content-Disposition" /attachment if (a != null) a = a.Substring(a.LastIndexOf("filename=") + 9); else a = FileName; byte buffer = this.ResponseAsBytes(Address, hwrp, Length, FileName); / lock (

28、_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, FromPosition, Length); ExceptionEventArgs eea = new Excep

29、tionEventArgs(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; internal byte ResponseAsBytes(string RequestURL, WebResponse Respons

30、e, 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

31、= 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);

32、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)

33、 / / this._bytes += num2; / if (num2 > 0) if (this.DataReceive != null) byte buffer = new bytenum2; Buffer.BlockCopy(buffer1, p, buffer, 0, buffer.Length); DownLoadState dls = new DownLoadState(RequestURL, Response.ResponseUri.AbsolutePath, FileName, a, P, num2, buffer); DownLoadEventArgs dlea =

34、new DownLoadEventArgs(dls); /觸發(fā)事件 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 bytenum3; Buffer.BlockCopy(buffer1, 0, buffer3, 0, num3); buffer1 = buffer3; re

35、turn 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, e

36、ea); 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 OnDataReceive(DownLoadEventArgs e) /觸發(fā)數(shù)據(jù)到達事件 DataReceive(this, e); public byte UploadFi

37、le(string address, string fileName) return this.UploadFile(address, "POST", fileName, "file"); public string UploadFileEx(string address, string method, string fileName, string fieldName) return Encoding.ASCII.GetString(UploadFile(address, method, fileName, fieldName); public byt

38、e UploadFile(string address, string method, string fileName, string fieldName) byte buffer4; FileStream stream1 = null; try fileName = Path.GetFullPath(fileName); string text1 = "-" + DateTime.Now.Ticks.ToString("x"); string text2 = "application/octet-stream" stream1 =

39、new FileStream(fileName, FileMode.Open, FileAccess.Read); WebRequest request1 = WebRequest.Create(this.GetUri(address); request1.Credentials = this.m_credentials; request1.ContentType = "multipart/form-data; boundary=" + text1; request1.Method = method; string textArray1 = new string7 &quo

40、t;-", text1, "rnContent-Disposition: form-data; name="" + fieldName + "" filename="", Path.GetFileName(fileName), ""rnContent-Type: ", text2, "rnrn" string text3 = string.Concat(textArray1); byte buffer1 = Encoding.UTF8.GetBytes(text3)

41、; byte buffer2 = Encoding.ASCII.GetBytes("rn-" + text1 + "rn"); long num1 = 0x7fffffffffffffff; try num1 = stream1.Length; request1.ContentLength = (num1 + buffer1.Length) + buffer2.Length; catch byte buffer3 = new byteMath.Min(0x2000, (int) num1); using (Stream stream2 = request

42、1.GetRequestStream() int num2; stream2.Write(buffer1, 0, buffer1.Length); do num2 = stream1.Read(buffer3, 0, buffer3.Length); if (num2 != 0) stream2.Write(buffer3, 0, num2); while (num2 != 0); stream2.Write(buffer2, 0, buffer2.Length); stream1.Close(); stream1 = null; WebResponse response1 = request

43、1.GetResponse(); buffer4 = this.ResponseAsBytes(response1); catch (Exception exception1) if (stream1 != null) stream1.Close(); stream1 = null; if (!(exception1 is WebException) && !(exception1 is SecurityException) /throw new WebException(SR.GetString("net_webclient"), exception1);

44、 throw new WebException("net_webclient", exception1); throw; return buffer4; private byte ResponseAsBytes(WebResponse response) int num2; long num1 = response.ContentLength; bool flag1 = false; if (num1 = -1) flag1 = true; num1 = 0x10000; byte buffer1 = new byte(int) num1; Stream stream1 =

45、 response.GetResponseStream(); int num3 = 0; do num2 = stream1.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; while (num2 != 0); stream1.C

46、lose(); if (flag1) byte buffer3 = new bytenum3; Buffer.BlockCopy(buffer1, 0, buffer3, 0, num3); buffer1 = buffer3; return buffer1; private NameValueCollection m_requestParameters; private Uri m_baseAddress; private ICredentials m_credentials = CredentialCache.DefaultCredentials; public ICredentials

47、Credentials get return this.m_credentials; set this.m_credentials = value; public NameValueCollection QueryString get if (this.m_requestParameters = null) this.m_requestParameters = new NameValueCollection(); return this.m_requestParameters; set this.m_requestParameters = value; public string BaseAd

48、dress get if (this.m_baseAddress != null) return this.m_baseAddress.ToString(); return string.Empty; set if (value = null) | (value.Length = 0) this.m_baseAddress = null; else try this.m_baseAddress = new Uri(value); catch (Exception exception1) throw new ArgumentException("value", excepti

49、on1); private Uri GetUri(string path) Uri uri1; try if (this.m_baseAddress != null) uri1 = new Uri(this.m_baseAddress, path); else uri1 = new Uri(path); if (this.m_requestParameters = null) return uri1; StringBuilder builder1 = new StringBuilder(); string text1 = string.Empty; for (int num1 = 0; num

50、1 < this.m_requestParameters.Count; num1+) builder1.Append(text1 + this.m_requestParameters.AllKeysnum1 + "=" + this.m_requestParametersnum1); text1 = "&" UriBuilder builder2 = new UriBuilder(uri1); builder2.Query = builder1.ToString(); uri1 = builder2.Uri; catch (UriForma

51、tException) uri1 = new Uri(Path.GetFullPath(path); return uri1; / <summary> / 測試類 / </summary> class AppTest int _k = 0; int _K = 0; static void Main() AppTest a = new AppTest(); Microshaoft.Utils.HttpWebClient x = new Microshaoft.Utils.HttpWebClient(); a._K = 10; /訂閱 DataReceive事件 x.Dat

52、aReceive += new Microshaoft.Utils.HttpWebClient.DataReceiveEventHandler(a.x_DataReceive); /訂閱 ExceptionOccurrs事件 x.ExceptionOccurrs += new Microshaoft.Utils.HttpWebClient.ExceptionEventHandler(a.x_ExceptionOccurrs); x.ThreadProcessEnd += new Microshaoft.Utils.HttpWebClient.ThreadProcessEventHandler(

53、a.x_ThreadProcessEnd); string F = "http:/localhost/download/phpMyAdmin-2.6.1-pl2.zip" F = " a._F = F; string f = F.Substring(F.LastIndexOf("/") + 1); /(new System.Threading.Thread(new System.Threading.ThreadStart(new ThreadProcessState(F, "E:temp" + f, 10, x).SThre

54、adProcess).Start(); x.DownloadFile(F, "d: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(); string bs = "" /

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論