1 Public stringDoPost (stringUrlstringdata)2 {3HttpWebRequest req = getwebrequest (URL,"POST");4 byte[] PostData =Encoding.UTF8.GetBytes (data);5Stream Reqstream =req. GetRequestStream ();6Reqstream.write (PostData,0, postdata.length);7 reqstream.close ();8HttpWebResponse RSP =(HttpWebResponse) req. GetResponse ();9Encoding Encoding =encoding.getencoding (RSP. CharacterSet);Ten returngetresponseasstring (RSP, encoding); One } A - PublicHttpWebRequest GetWebRequest (stringUrlstringmethod) - { theHttpWebRequest req =(HttpWebRequest) webrequest.create (URL); -Req. Servicepoint.expect100continue =false; -Req. ContentType ="Application/x-www-form-urlencoded;charset=utf-8"; -Req. ContentType ="Text/json"; +Req. Method =method; -Req. KeepAlive =true; +Req. UserAgent ="MySoft"; AReq. Timeout =1000000; atReq. Proxy =NULL; - returnreq; - } - - Public stringgetresponseasstring (HttpWebResponse RSP, Encoding Encoding) - { inStringBuilder result =NewStringBuilder (); -Stream stream =NULL; toStreamReader reader =NULL; + Try - { the //reads the HTTP response as a stream of characters *stream =RSP. GetResponseStream (); $Reader =NewStreamReader (stream, encoding);Panax Notoginseng //each read is not greater than 256 characters and is written to a string - Char[] buffer =New Char[ the]; the intReadbytes =0; + while(readbytes = reader.) Read (Buffer,0, buffer. Length)) >0) A { theResult. Append (Buffer,0, readbytes); + } - } $ finally $ { - //Freeing Resources - if(Reader! =NULL) reader. Close (); the if(Stream! =NULL) stream. Close (); - if(RSP! =NULL) RSP. Close ();Wuyi } the - returnresult. ToString (); Wu}
C # Post mode transmission message, and processing response