C # Post Get method Send Request

Source: Internet
Author: User

HttpPost Send request without parameters

1         /// <summary>2         ///POST request with no parameters3         /// </summary>4          Public voidHttppostnoparam ()5 {6              stringURL ="Request Address";7HttpWebRequest request =webrequest.createhttp (URL);8Request. Method ="POST";9Request. ContentType ="application/x-www-form-urlencoded";TenStream Myrequeststream =request. GetRequestStream (); OneStreamWriter Mystreamwriter =NewStreamWriter (Myrequeststream, Encoding.GetEncoding ("gb2312")); A mystreamwriter.close (); - myrequeststream.close (); -            theHttpWebResponse response =(HttpWebResponse) request. GetResponse (); -Stream Myresponsestream =Response. GetResponseStream (); -StreamReader Mystreamreader =NewStreamReader (Myresponsestream, Encoding.GetEncoding ("Utf-8")); -              stringRetString =mystreamreader.readtoend (); + mystreamreader.close (); - myresponsestream.close (); +}

HttpPost way to send request with parameters

1           /// <summary>2          ///Post request with parameters
UserID, transflow parameter name3 /// </summary>4 Public voidHttppostparam ()5{stringURL ="Request Address";6HttpWebRequest request =webrequest.createhttp (URL);7Request. Method ="POST";8Request. ContentType ="application/x-www-form-urlencoded";9 stringparam ="userid=2098&transflow=2";Ten byte[] by; Oneby =System.Text.Encoding.UTF8.GetBytes (param); ARequest. ContentLength =by . Length; -Stream Myrequeststream =request. GetRequestStream (); -StreamWriter Mystreamwriter =NewStreamWriter (Myrequeststream, Encoding.GetEncoding ("gb2312")); theMyrequeststream.write (By,0, by. Length); - mystreamwriter.close (); - myrequeststream.close (); - + -HttpWebResponse response =(HttpWebResponse) request. GetResponse (); +Stream Myresponsestream =Response. GetResponseStream (); AStreamReader Mystreamreader =NewStreamReader (Myresponsestream, Encoding.GetEncoding ("Utf-8")); at stringRetString =mystreamreader.readtoend (); - mystreamreader.close (); - myresponsestream.close (); -}

HttpGet way to send a request

1         /// <summary>2         ///Get mode3         /// </summary>4          Public voidHttpGet ()5         {6             stringURL =the request address? id=2";7HttpWebRequest request =webrequest.createhttp (URL);8Request. Method ="Get";9Request. ContentType ="application/x-www-form-urlencoded";Ten  OneHttpWebResponse response =(HttpWebResponse) request. GetResponse (); AStream Read =Response. GetResponseStream (); -StreamReader SM =NewStreamReader (read, System.Text.Encoding.UTF8); -             stringReadString =SM. ReadToEnd (); the  -}

C # Post Get method Send Request

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.