C # written by HTTPREQUSETRESPONSE class, asynchronous, event ... is also hot!

zhaozj2021-02-08  290

// Call example: / * 2003-7-11. Support Proxy2. You can specify synchronous or asynchronous 3. Some small modifications * / private void button1_click (Object sender, system.eventargs e)? {?? httprequestResponse xx = new httpRequestResponse ); ?? xx.DataArrival = new DataArrivalEventHandler (xx_DataArrival); ?? // xx.SendRequest (@ "http: // localhost: 2080 / examples / myservlets / servlet2", "message = QQ & desttel = 13910911925 & mytel = 13910081138 & mypwd = 263996211 "); ?? xx.sendRequest (@" http: // localhost "," Message = Hello QQ & Desttel = 13910911925 & mytel = 13910081138 & myPwd = 263996211 "," get ", true); ?? xx.sendRequest (" http: // ? localhost / quickstart / util / srcview.aspx path = / quickstart / howto / samples / net / WebRequests / clientGETasync.src "," post ", false) ;?} private int i = 0; private void xx_DataArrival (object sender, DataArriValeventargs e)? {?? console.writeLine (i ); ?? console.write (E.RecievedData?); ?? if (e.iscomplete) ??? {??? console.write ("[" (HttprequestResponse) sender) .Request.cookiecontainer.getCookieHeader ((httprequestResponse) sender .response.responseuri) .tostring () "]"); ???} ??}

/ /================================//class1.cs

Using system.net; using system.text; using system.io;

? Public class RequestState {const int BUFFER_SIZE = 1024;? Public System.Text.StringBuilder RequestData;? Public byte [] BufferRead;? Public System.Net.HttpWebRequest Request;? Public System.IO.Stream ResponseStream;? Public System.Text .Decoder StreamDecode = System.Text.Encoding.GetEncoding ( "gb2312") GetDecoder () ;? public RequestState () {?? BufferRead = new byte [BUFFER_SIZE];.? ?? RequestData = new System.Text.StringBuilder ( " "); ?? Request = null; ?? ResponseStream = null;}} public delegate void DataArrivalEventHandler (object sender, DataArrivalEventArgs e); public class DataArrivalEventArgs:??? System.EventArgs {private string mRecievedData; private bool mIsComplete = false; PUBLIC DATAARRIVALEVENTARGS (STRING DATA, BOOL COMPLETE)? {?? mrecievedData = data; ?? Miscomplete = completion;?}? public string recieveddata? {?? get ?? {???}?}? Public Bool IsComplete? {?? get ?? {??? Return Miscomplete; ??}?}} Class httprequestResponse} Class httprequestResponse {? public static system.threading.manualRetevent Alldone = New System.Threa Ding.manualReveTevent (false) ;? const Int buffer_size = 1024;? public evenet datarivalenthandler data

?? Protected virtual void OnDataArrival (DataArrivalEventArgs e) {?? if (! DataArrival = null) ?? {??? DataArrival (this, e);? ??}}? Private System.Text.Encoding mRequestEncoding = System.Text .Encoding.getencoding ("GB2312");? Private bool museproxy = false;? Public bool usproxy? {?? get ?? {???} ?? set ?? {??? museproxy = value ;??}?}

? private system.net.WebProxy mwp = system.net.WebProxy.getDefaultProxy () ;? public system.net.WebProxy proxy? {?? get ?? {??? Return MWP; ??} ?? set ?? { ??? MWP = value; ??}?}

? Private System.Net.HttpWebRequest mHWReq = null;? Public System.Net.HttpWebRequest Request {?? get ?? {??? return mHWReq;? ??}}? ?? private System.Net.HttpWebResponse mHWRsp = null; ? public System.Net.HttpWebResponse Response {?? get ?? {??? return mHWRsp; ??}?}? public void SendRequest (string RequestURL, string RequestData, bool Async) {?? SendRequest (RequestURL, RequestData?? ??, "GET", Async) ;?} public void SendRequest (string RequestURL, string RequestData, string RequestMethod, bool Async) {?? HttpWebRequest req; ?? RequestState rs = new RequestState (); ?? if (RequestData! = NULL) ?? {??? INT i = 0, J; ??? char [] reserved = {'?', '=', '&'}; ??? While (i ??? {

???? j = RequestData.indexofany (Reserved, i);

???? IF (j == - 1)

???? {

????? rs.RequestData.Append (System.Web.httputility.urlencode (RequestData.Substring (I, RequestData.Length-i), MREQUESTENCODING);

????? Break;

????}

???? rs.RequestData.Append (System.web.httputility.urlencode (RequestData.Substring (i, j-i), mrequestencoding);

???? rs.RequestData.Append (RequestData.substring (j, 1));

???? i = j 1;

???}

??}

?? IF (RequestMethod.toupper () == "post")

?? {

??? byte [] buffer = null;

??? buffer = MREQUESTENCODING.GETBYTES (rs.RequestData.toString ());

??? REQ = (httpwebRequest) WebRequest.create (RequestURL);

??? req.contentLength = Buffer.Length;

??? req.method = requestmethod;

??? req.conteType = "Application / X-WWW-form-urlencoded";

??? stream newstream = req.getRequestStream ();

??? NewStream.write (buffer, 0, buffer.length);

??? newstream.close ();

??}

?? Else

?? {

??? REQ = (httpwebRequest) WebRequest.create (RequestURL " rs.RequestData.toString ());

??}

?? req.credentials = credentialcache.defaultcredentials; ?? Req.USERAGENT = "

Playyuer@microshaoft.com ";

?? Requisel = true;

?? Req.Headers.Set ("Pragma", "no-cache");

?? REQ.TIMEOUT = 300000;

?? IF (MuseProxy)

?? {

??? if (MWP == null)

???? MWP = system.net.webproxy.getDefaultproxy ();

??? GlobalProxySelection.select = MWP;

??}

?? rs.Request = Req;

?? MHWREQ = Req;

??

?? System.iasyncResult r = (System.iasyncResult) Req.BegingeTResponse (New System.asyncCallback (ResponseCallback), RS);

?? IF (! async)

??? alldone.waitone ();

?

?? Private void ResponseCallback (IAsyncResult ar) {?? RequestState rs = (RequestState) ar.AsyncState; ?? System.Net.HttpWebRequest req = rs.Request; ?? System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse ) req.EndGetResponse (ar); ?? System.IO.Stream ResponseStream = resp.GetResponseStream (); ?? rs.ResponseStream = ResponseStream; ?? IAsyncResult iarRead = ResponseStream.BeginRead (rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback (ReadCallback), RS);?}

转载请注明原文地址:https://www.9cbs.com/read-1083.html

New Post(0)