// Call example:
/*
2003-7-1
1. Support for proxy
2. synchronous or asynchronous
3. Some minor changes
*/
Private void button#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;
Using system. net;
Using system. Threading;
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 = complete;
?}
? Public String recieveddata
? {
?? Get
?? {
??? Return mrecieveddata;
??}
?}
? Public bool iscomplete
? {
?? Get
?? {
??? Return miscomplete;
??}
?}
}
Class httprequestresponse
{
? Public static system. Threading. manualresetevent alldone = new system. Threading. manualresetevent (false );
? Const int buffer_size = 1024;
? Public event dataarrivaleventhandler dataarrival;
? 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 useproxy
? {
?? Get
?? {
??? Return museproxy;
??}
?? 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. contenttype = "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 ";
?? Req. keepalive = true;
?? Req. headers. Set ("Pragma", "No-Cache ");
?? Req. Time out = 300000;
?? If (museproxy)
?? {
??? If (MWP = NULL)
???? MWP = system. net. WebProxy. getdefaproxy proxy ();
??? 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);
?}
? Private void readcallback (iasyncresult asyncresult)
? {
?? Requeststate rs = (requeststate) asyncresult. asyncstate;
?? System. Io. Stream responsestream = Rs. responsestream;
?? Int READ = responsestream. endread (asyncresult );
?? If (read> 0)
?? {
??? Char [] charbuffer = new char [buffer_size];
??? Int Len = Rs. streamdecode. getchars (Rs. bufferread, 0, read, charbuffer, 0 );
??? System. String STR = new system. String (charbuffer, 0, Len );
??? Ondataarrival (New dataarrivaleventargs (STR, false ));
??? Rs. requestdata. append (STR );
??? Iasyncresult AR = responsestream. beginread (Rs. bufferread, 0, buffer_size, new asynccallback (readcallback), RS );
??}
?? Else
?? {
??? If (Rs. requestdata. length> 1)
??? {
???? String strcontent;
???? Strcontent = Rs. requestdata. tostring ();
???}
??? Responsestream. Close ();
??? Alldone. Set ();
??? Ondataarrival (New dataarrivaleventargs ("/N", true ));
??}
?? Return;
?}
}