1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Net;6 usingSystem.Text;7 usingSystem.IO;8 9 namespaceTestrsaTen { One Public classgetdatabyhttp A { - - the Public Static stringDoPost (stringUrlstringdata) - { -HttpWebRequest req = getwebrequest (URL,"POST"); - byte[] PostData =Encoding.UTF8.GetBytes (data); +Stream Reqstream =req. GetRequestStream (); -Reqstream.write (PostData,0, postdata.length); + reqstream.close (); AHttpWebResponse RSP =(HttpWebResponse) req. GetResponse (); atEncoding Encoding =encoding.getencoding (RSP. CharacterSet); - returngetresponseasstring (RSP, encoding); - } - - Public StaticHttpWebRequest GetWebRequest (stringUrlstringmethod) - { inHttpWebRequest req =(HttpWebRequest) webrequest.create (URL); -Req. Servicepoint.expect100continue =false; toReq. ContentType ="Application/x-www-form-urlencoded;charset=utf-8"; +Req. ContentType ="Text/json"; -Req. Method =method; theReq. KeepAlive =true; *Req. UserAgent ="Guanyisoft"; $Req. Timeout =1000000;Panax NotoginsengReq. Proxy =NULL; - returnreq; the } + A Public Static stringgetresponseasstring (HttpWebResponse RSP, Encoding Encoding) the { +StringBuilder result =NewStringBuilder (); -Stream stream =NULL; $StreamReader reader =NULL; $ Try - { - //reads the HTTP response as a stream of characters thestream =RSP. GetResponseStream (); -Reader =NewStreamReader (stream, encoding);Wuyi //each read is not greater than 256 characters and is written to a string the Char[] buffer =New Char[ the]; - intReadbytes =0; Wu while(readbytes = reader.) Read (Buffer,0, buffer. Length)) >0) - { AboutResult. Append (Buffer,0, readbytes); $ } - } - finally - { A //Freeing Resources + if(Reader! =NULL) reader. Close (); the if(Stream! =NULL) stream. Close (); - if(RSP! =NULL) RSP. Close (); $ } the the returnresult. ToString (); the } the - in the } the } About
View Code
WebService interface gets data over HTTP