/ * .NET / C #: Implement the HTTP web client tool class that supports breakpoints to renew multi-thread download (C # diy httpwebclient) * Reflector has a case of system.net.WebClient, rewriting, or adds a number of: * Download, Upload related methods ! * Download related changes! * Added DataReceive, ExceptionOccurrs event! * Understand the HTTP protocol of the server-side interaction with the client to see: * Let the file download custom connection support FlashGet breakpoint renewable multi-thread link download! JSP / Servlet implementation! * Http://blog.9cbs.net/playyueer/archive/2004/08/02/58430.aspx * Make the file download custom connection supports FlashGet breakpoint renewal multi-thread link download! C # / ASP.NET implementation! * Http://blog.9cbs.net/playyuer/archive/2004/08/02/58281.aspx * /
namespace Microshaoft.Utils {using System; using System.IO; using System.Net; using System.Text; using System.Security; using System.Threading; using System.Collections.Specialized; ///
Private string _ttachmentname; 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 {return _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._FileName = 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 Position, int Length, ThreadCallbackHandler tch) {this._RequestURL = RequestURL THIS._RESPONSEURL = responseURL; this._filename = filename; this._attachmentname = attachmentname; this._position = position; this._ens = length; this._threadcallback = tch;}
internal DownLoadState (string RequestURL, string 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_threadcallback;
// INTERNAL VOID StartDownloadFilechunk () {if (this._threadcallback! = Null) {this._threadcallback (this._requesturl, this._filename, this._position, this._length);}}
}
/ / Enter the method of executing the proxy thread signing uniform public delegate Void ThreadCallbackHandler (String S, String S, INT I, INT i);
// Abnormal processing action public enum exceptionActions {throw, cacall, ignore, retry}
///
Public DownloadState DownloadState {Get {Return_downloadState;}}
Public Exception Exception {get {return _exception;}}
PUBLIC EXCETIONACTIONES EXCETIONACTION {Get {Return _ExceptionAction;} set {_exceptionAction = value;}}
INTERNAL EXCEPTIONEVENTARGS (System.Exception E, DownloadState DownloadState) {this._exception = E; this._downloadstate = DownloadState;}}
///
Public DownloadState DownloadState {Get {Return_downloadState;}}
Public DownloadState {THIS._DOWNLOADSTATATE = DownloadState;
}
///
Public Delegate Void DataReceiveEventHandler (HttpWebClient Sender, DownloadEventAndargs E);
Public Event DataReceiveEventHandler DataReceive; // Receive Byte Data Event
Public Delegate Void ExceptionEventHandler (HttpWebClient Sender, ExceptionEventArgs E);
Public Event ExceptionEventHandler ExceptionOccurrs; // A abnormal event occurs
Private int _fileLength; // Download the total size of the file
Public int filelength {get {return _fileLength;}}
///
L = ((l == -1) || (L> 0x7fffffff))? ((Long) 0x7fffffff): L; //int32.maxvalue This constant is 2,147,483,647; ie hexadecimal 0x7fffffffFFFFFFFFFFFFFFFFFF
INT L = (int) L;
THIS._FILENGTH = L;
// In the local predetermined space (actually do not have a predetermined space in multi-thread) // 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]! = 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) // block size is at least 128 k byte {s = 2 * 64 * 1024;} ss = s; INT i = 0; While (L> S) {L - = S; if (l 0) {DownloadState X = New DownloadState (address, hwrp.responseuri.absolutePath , FileName, a, p, s, new ThreadCallbackHandler (this.DownloadFileChunk)); // threaded download // x.StartDownloadFileChunk (); // multithreaded download // thread t = new thread (new ThreadStart (x.StartDownloadFileChunk ))))). START (); //t.Start ();} p = s;} s = ss; byte [] buffer = this.Responseasbytes (Address, HWRP, S, FileName);
// 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) &&! ("net_webclient", e);} throw;}}
}
///
IF (EA == ExceptionActions.throw) {if (! (e is webexception) &&! ("Net_WebClient", E);} through;}}}
INTERNAL BYTE [] Responseasbytes (String Requesturl, WebResponse Response, long length, string filename) {string a = null; // attachmentname int P = 0; // The position pointer of the entire file 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; // This block of position pointer
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.getResponseSstream (); 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; //} f (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); // Trigger event this.ondatareceive (DLEA); //system.threading.thread.sleep(100);} P = Num2; // This block position pointer P = Num2; // The position pointer of the entire file} Else {Break;
} While (Num2! = 0);
S.close (); s = null; if (flag1) {byte [] buffer3 = new byte [Num3]; buffer1, 0, buffer3, 0, num 3); 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); Exceptionocurrs (this, EEA); EA = EEA.Exception;
IF (EA == ExceptionActions.throw) {if (! (e is webexception) &&! ("Net_WebClient", E);} throw;}}}}}}} Return NULL
Private void OnDataReceive (DownloadEventArgs E) {// Trigger data arrival event DataReceive (this, e);}
Public Byte [] UPLOADFILE (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 byte [] 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 = 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 string [7] {"-", text1, "/ r / ncontent-disposition: form-data; name = /" fieldname "/"; filename = / "" ", Path.getfilename (filename)," / "/" / r / nContent-type: ", text2," / r / n / r / n "}; string text3 = string.concat (TextArray1); byte [] Buffer1 = encoding.utf8.getbytes (text3); byte [] buffer2 = encoding.ascii.getbytes ("/ r / n" text1 "/ r / n"); long Num1 = 0x7fffffffffffffffff; Try {Num1 = Stream1.Length; Request1.contentLength = (Num1 Buffer1.Length) Buffer2.Length;} catch {} byte [] buffer3 = new byte [Math.min (0x2000, (int) Num1)]; use)]; use)]; using (stream stream2 = Request1.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 = request1.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); 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 = response.getResponseSstream (); 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.close (); if (flag1) {byte [] buffer3 = new byte [Num3]; buffer1, 0, buffer3, 0, num3); buffer1 = buffer3;} Return buffer1;} private namevaluecollection m_requestparameters; private uri m_baseaddress; private icredentials m_credentials = credentialcache.defaultcredentials;
Public ictedentials credentials {get {returnim_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 baseaddress {get {if (this.m_baseaddress! = null) {returnim_baseaddress.tostring ();} return string.empty;} set {i ((value == null) || (value.length == 0)) {this.m_baseAddress = null;} else {try {this.m_baseAddress = new Uri (value);} catch (Exception exception1) {throw new ArgumentException ( "value", exception1);}}}} 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; num1 } } /// // Subscribe DataReceive event x.DataReceive = new Microshaoft.Utils.HttpWebClient.DataReceiveEventHandler (a.x_DataReceive); // subscribe ExceptionOccurrs event x.ExceptionOccurrs = new Microshaoft.Utils.HttpWebClient.ExceptionEventHandler (a.x_ExceptionOccurrs); 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: // localhost: 1080 / test / demary.jsp"; // f = "http://localhost/download/webcast20050125_ppt.zip"; // f = "http://www.morequick.com/greenbrowsergb.zip"; // f = "http: // localhost / download / Test_local.rar "; string f = f.substring (f.lastIndexof (" / ") 1); // (New System.Threading.Thread (New System.Threading.ThreadStart (New ThreadProcessState (f, @ "E: / TEMP /" F, 10, x) .startthreadProcess))))))). Start (); x.downloadfile (f, @ "e: / temp / temp /" f, 10); // x.downloadfilechunk (f, @ "E: / TEMP /" F, 15, 34556); System.console.readline (); // upload test // 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 = ""; // Used to record the last bit number BOOL B = false; private int i = 0; private static object _synclockObject = new object (); string _f; string _f; private void x_DataReceive (Microshaoft.Utils.HttpWebClient Sender, Microshaoft.Utils.DownLoadEventArgs e) {if (! this.b) {lock (_SyncLockObject) {if (! this.b) {System.Console.Write (System.DateTime. Now.toTString () "has received data:"); //system.console.write (system.datetime.now.tostring () "has received data:"); this.b = true;}} 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.filecess.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.tostring (); lock.tockObject THIS.I = E.DownloadState.Data.Length; System.Console.write (BS "/ B / B / B / B / B / B / B / B / B / B / B" i "/" Sender.fileLength "byte data" s); //system.console.write (BS i "byte data" s); this.bs = new string ('/ b', DIGITS (i) 3 Digits (Sender.FileLength) S.Length);}} INT DIGITS (INT N) // Number of places {n = system.math.abs (n = n / 10; int i = 1; while (n> 0) {n = N / 10; i } Return i;} private void x_ExceptionOccurrs (Microshaoft.Utils.HttpWebClient Sender, Microshaoft.Utils.ExceptionEventArgs e) {System.Console.WriteLine (e.Exception.Message); corresponds to an abnormal re-download HTTP // happens, you can choose their own treatment or the discretion Microshaoft.Utils.HttpWebClient x = new Microshaoft.Utils.HttpWebClient (); x.DownloadFileChunk (this._F, this._f, e.DownloadState.Position, e.DownloadState.Length); e.ExceptionAction = Microshaoft.Utils.exceptionActions.ignore;}} / * * Action php: http://localhost/phpmyadmin/uploadAction.php: "; if (Move_uploaded_file ($ _ files ['file1'] ['TMP_NAME'], $ UPLOADFILE)) {Print "File is Valid, and Was successfully uploaded. "} Else {print" Possible File Upload Attack! Here's Some Debugging Info: / N "; Print_r ($ _ files);} print" pre> ";?> Body> html> * / 2005- 03 -14 Revision:
/ * .NET / C #: Implement the tool class for the breakpoint to renew multi-thread downloads * Reflector has a slice of system.Net.WebClient, overloaded or adds a number of: * Download, Upload related methods! * Added DataReceive, ExceptionOccurrs event * /
Namespace microshaoft.utils {using system; using system.net; using system.text; using system.threading; using system.chreading; using system.collections.specialize;
///
Private string _ttachmentname; private int _position; private string _RequestURL; private string _responseurl; private int _Length; private bote [] _data
Public string filename {get {return _filename;}}
Public int position {get {return _Position;}}
Public Int length {get {return _length;}}
Public string attachmentname {get {return _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._FileName = 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 Position, int Length, ThreadCallbackHandler tch) {this._RequestURL = RequestURL; this._ResponseURL = ResponseURL; this._FileName = FileName; this._AttachmentName = AttachmentName; this ._Position = position; this._gength = length; this._threadcallback = tch;}
internal DownLoadState (string RequestURL, string 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 threadcallback;
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._ThreadCallback (this._RequestURL, this._FileName, this._Position, this._Length); this._hwc.OnThreadProcess (this._thread); }
}
/ / Enter the method of executing the proxy thread signing uniform public delegate Void ThreadCallbackHandler (String S, String S, INT I, INT i);
// Abnormal processing action public enum exceptionActions {throw, cacall, ignore, retry}
///
Private DownloadState_downloadstate;
Public DownloadState DownloadState {Get {Return_downloadState;}}
Public Exception Exception {get {return _exception;}}
PUBLIC EXCETIONACTIONES EXCETIONACTION {Get {Return _ExceptionAction;} set {_exceptionAction = value;}}
internal ExceptionEventArgs (System.Exception e, DownLoadState DownloadState) {this._Exception = e; this._DownloadState = DownloadState;}} ///
Public DownloadState DownloadState {Get {Return_downloadState;}}
Public DownloadState {THIS._DOWNLOADSTATATE = DownloadState;
}
Public Class ThreadProcessEventArgs: system.eventargs {private thread_thread;
Public thread thread {get {return this._thread;}}
Public ThreadProcessEventArgs (thread thread) {this._thread = thread;
}
///
Public Delegate Void DataReceiveEventHandler (HttpWebClient Sender, DownloadEventAndargs E);
Public Event DataReceiveEventHandler DataReceive; // Receive Byte Data Event
Public Delegate Void ExceptionEventHandler (HttpWebClient Sender, ExceptionEventArgs E);
Public Event ExceptionEventHandler ExceptionOccurrs; // A abnormal event occurs
Public Delegate Void ThreadProcessEventHandler (HttpwebClient Sender, ThreadProcessEventArgs E);
Public Event ThreadProcessEventHandler ThreadProcessend; // Multi-threaded tension event
Private int _fileLength; // Download the total size of the file
Public int filelength {get {return _fileLength;}}
///
L = ((l == -1) || (L> 0x7fffffff))? ((Long) 0x7fffffff): L; //int32.maxvalue This constant is 2,147,483,647; ie hexadecimal 0x7fffffffFFFFFFFFFFFFFFFFFF
INT L = (int) L;
THIS._FILENGTH = L;
// In the local predetermined space (actually do not have a predetermined space in multi-thread) // 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]! = 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) // block size is at least 128 k byte {s = 2 * 64 * 1024;} ss = s; INT i = 0; While (L> S) {L - = S; if (l 0) {DownloadState X = New DownloadState (address, hwrp.responseuri.absolutePath , FileName, a, p, s, new ThreadCallbackHandler (this.DownloadFileChunk)); // threaded download // x.StartDownloadFileChunk (); x.httpWebClient = this; // multithreaded download thread t = new thread (new ThreadStart (x.startdownloadfilechunk); //this.ontreadprocess (T.Start ();
} P = s;} s = ss; byte [] buffer = this.Responseasbytes (Address, HWRP, S, FileName); this.ontreadProcess (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) &&! ("net_webclient", e);} throw;}}
}
Internal Void OnthreadProcess (ThreadProcessend! = null) {ThreadProcessEventArgs TPEA = New ThreadProcessEventArgs (T); ThreadProcessend (THIS, TPEA);}}
///
BYTE [] Buffer = this.responseasbytes (address, hwrp, length, filename); // 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, FromPosition, Length); ExceptionEventArgs eea = new ExceptionEventArgs (e, x); ExceptionOccurrs (this, EEA); EA = EEA.Exception;
IF (EA == ExceptionActions.throw) {if (! (e is webexception) &&! ("Net_WebClient", E);} through;}}}
INTERNAL BYTE [] Responseasbytes (String Requesturl, WebResponse Response, long length, string filename) {string a = null; // attachmentname int P = 0; // The position pointer of the entire file 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; // This block of position pointer
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.getResponseSstream (); 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; //} f (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); // Trigger event this.ondatareceive (DLEA); //system.threading.thread.sleep(100);} P = Num2; // This block position pointer P = Num2; // The position pointer of the entire file} Else {Break;
} While (Num2! = 0);
S.close (); s = null; if (flag1) {byte [] buffer3 = new byte [Num3]; buffer1, 0, buffer3, 0, num 3); 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); Exceptionocurrs (this, EEA); EA = EEA.Exception;
IF (EA == ExceptionActions.throw) {if (! (e is webexception) &&! ("Net_WebClient", E);} throw;}}}}}}} Return NULL
Private void OnDataReceive (DownloadEventArgs E) {// Trigger data arrival event DataReceive (this, e);}
Public Byte [] UPLOADFILE (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 byte [] 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 = 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 string [7] {"-", text1, "/ r / ncontent-disposition: form-data; name = /" fieldname "/"; filename = / "" ", Path.getfilename (filename)," / "/" / r / nContent-type: ", text2," / r / n / r / n "}; string text3 = string.concat (TextArray1); byte [] Buffer1 = encoding.utf8.getbytes (text3); byte [] buffer2 = encoding.ascii.getbytes ("/ r / n" text1 "/ r / n"); long Num1 = 0x7fffffffffffffffff; Try {Num1 = Stream1.Length; Request1.contentLength = (Num1 Buffer1.Length) Buffer2.Length;} catch {} byte [] buffer3 = new byte [Math.min (0x2000, (int) Num1)]; use)]; use)]; using (stream stream2 = Request1.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 = request1.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); 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 = response.getResponseSstream (); 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.close (); if (flag1) {byte [] buffer3 = new byte [Num3]; buffer1, 0, buffer3, 0, num3); buffer1 = buffer3;} Return buffer1;} private namevaluecollection m_requestparameters; private uri m_baseaddress; private icredentials m_credentials = credentialcache.defaultcredentials;
Public ictedentials credentials {get {returnim_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 baseaddress {get {if (this.m_baseaddress! = null) {returnim_baseaddress.tostring ();} return string.empty;} set {i ((value == null) || (value.length == 0)) {this.m_baseAddress = null;} else {try {this.m_baseAddress = new Uri (value);} catch (Exception exception1) {throw new ArgumentException ( "value", exception1);}}}} 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; num1 } } /// Static void main () {apptest a = new apptest (); microshaoft.utils.httpwebclient x = new microshaoft.utils.httpwebclient (); A._K = 10; // Subscribe DataReceive event x.DataReceive = new Microshaoft.Utils.HttpWebClient.DataReceiveEventHandler (a.x_DataReceive); // subscribe ExceptionOccurrs event x.ExceptionOccurrs = new Microshaoft.Utils.HttpWebClient.ExceptionEventHandler (a.x_ExceptionOccurrs); x. ThreadProcessEnd = new Microshaoft.Utils.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: // localhost: 1080 / test / demary.jsp"; // f = "http://localhost/download/webcast20050125_ppt.zip"; // f = "http://www.morequick.com/greenbrowsergb.zip"; // f = "http: // localhost / download / Test_local.rar "; string f = f.substring (f.lastIndexof (" / ") 1); // (New System.Threading.Thread (New System.Threading.ThreadStart (New ThreadProcessState (f, @ "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 (); String BS = ""; // Used to record the last bit number BOOL B = false; private int = 0; private static object _synclockObject = new object (); string _f; string _f; private void x_datareceive (microshaoft.utils .Httpwebclient sender, microshaoft.utils.downloadloadEventArgs e) {if (! This.b) {lock (_SynclockObject) {if (! This.b) {system.console.write (system.datetime.now.tostring () " The data has been received: "); //system.console.write (system.datetime.now.write () " has received data: "); 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.filecess.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.tostring (); lock.tockObject THIS.I = E.DownloadState.Data.Length; System.Console.write (BS "/ B / B / B / B / B / B / B / B / B / B / B" i "/" Sender.fileLength "byte data" s); //system.console.write (BS i "byte data" s); this.bs = new string ('/ b', DIGITS (i) 3 Digits (Sender.FileLength) S.Length);}} INT DIGITS (INT N) // Number of places {n = system.math.abs (n = n / 10; int i = 1; while (n> 0) {n = N / 10; i } Return i;} private void x_ExceptionOccurrs (Microshaoft.Utils.HttpWebClient Sender, Microshaoft.Utils.ExceptionEventArgs e) {System.Console.WriteLine (e.Exception.Message); corresponds to an abnormal re-download HTTP // happens, you can choose their own treatment Microshaoft.Utils.HttpWebClient x = new Microshaoft.Utils.HttpWebClient (); x.DownloadFileChunk (this._F, this._f, e.DownloadState.Position, e.DownloadState.Length); e.ExceptionAction = Microshaoft.Utils .ExceptionActions.Ignore;} private void x_ThreadProcessEnd (Microshaoft.Utils.HttpWebClient Sender, Microshaoft.Utils.ThreadProcessEventArgs e) {// if (e.thread.ThreadState == System.Threading.ThreadState.Stopped) if (this._k == this._k - 1) System.console.writeline ("/ NEND");}}