Publicstring post_http (string url, string postdata) {string Strresult= "";Try{//converting a string parameter to a byte array facilitates writing to a streambyte[] Bytedata = encoding.getencoding ("Utf-8"). GetBytes (PostData); //Create a request instanceHttpWebRequest Myrequest =(HttpWebRequest) webrequest.create (URL);//Set Request ModeMyrequest.method = "POST";//set the content type of the requested data being sentMyrequest.contenttype = "application/x-www-form-urlencoded";//sets the content length of the requested data being sent. Myrequest.contentlength =bytedata.length;//get the requested streamStream Requeststream =Myrequest.getrequeststream ();//write content to the request streamrequestStream.Write (bytedata, 0, bytedata.length); Requeststream.close ();//get the content requestedHttpWebResponse Httpwresp =(HttpWebResponse) myrequest.getresponse ();//writes the resulting data to the streamStream Streamresp =Httpwresp.getresponsestream ();//Initializes a read file stream objectStreamReader sr =NewStreamReader (Streamresp, Encoding.UTF8); StringBuilder Strbuilder=NewStringBuilder ();//reading Data while(-1! =Sr. Peek ()) {Strbuilder.append (Sr.) ReadLine ());} Strresult=strbuilder.tostring (); Httpwresp.close (); Sr. Close ();}Catch(Exception ee) {strresult=EE. Message;}returnStrresult;}
 
2.get mode
Publicstring Get_http (String turl) {string strresult;Try{//instantiating WebRequest object staticHttpWebRequest HWR =(HttpWebRequest) httpwebrequest.create (Turl); Hwr. Method= "GET";//the length of milliseconds before a request is set to time outHwr. Timeout = 20000;//20s//Override to return a response to an Internet request. HWRHttpWebResponse Hwrs =(HttpWebResponse) hwr. GetResponse ();//returns the System.IO.Stream used to write data to an Internet resource. Stream MyStream =Hwrs. GetResponseStream ();//Initializes a stream object and sets the encoding format for initializationStreamReader sr =NewStreamReader (MyStream, Encoding.UTF8); StringBuilder SB=NewStringBuilder ();//determine if there is a value-1 no while(-1! =Sr. Peek ()) {//reads a row from the current stream and returns the data as a string. ReadLine ()//append to StringBuilder.Sb. Append (Sr. ReadLine () + "\ r \ n");} Strresult=sb. ToString ();//Close ObjectSr. Close (); Hwrs. Close ();}Catch(Exception ee) {strresult=EE. Message;}//return Resultsreturnstrresult;}
/span>
HTTP get,post way to access URLs