HTTP get,post way to access URLs

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.