. net/c# HttpWebRequest Transmit and receive parameters

Source: Internet
Author: User

1  Public stringPostData (stringUrlstringData//URL: Address to be sent to the website data: Transfer Required parameters2 {  3HttpWebRequest Myrequest =(HttpWebRequest) webrequest.create (URL); 4Myrequest.method ="POST"; 5Myrequest.contenttype ="application/x-www-form-urlencoded"; 6Myrequest.contentlength =data.  Length; 7Stream newstream =Myrequest.getrequeststream (); 8 //Send the data. 9ASCIIEncoding encoding =Newasciiencoding (); Ten byte[] PostData =encoding.  GetBytes (data);  OneNewstream.write (PostData,0, data.  Length);  A Newstream.close ();  - //Get Response -HttpWebResponse Myresponse =(HttpWebResponse) myrequest.getresponse ();  theStreamReader reader =NewStreamReader (Myresponse.getresponsestream (), Encoding.GetEncoding ("gb2312"));  - stringContent = Reader. ReadToEnd ();//Get Results -    - returnContent

Get method:

1Uri uri =NewUri (URL +"?"+postdata); 2System.Net.HttpWebRequest request =(System.Net.HttpWebRequest) System.Net.WebRequest.Create (URI); 3Request. Method ="GET"; 4Request. ContentType ="application/x-www-form-urlencoded"; 5Request. AllowAutoRedirect =false; 6Request. Timeout = the; 7System.Net.HttpWebResponse response =(System.Net.HttpWebResponse) request.  GetResponse (); 8Stream Responsestream =Response.  GetResponseStream (); 9StreamReader Readstream =NewStreamReader (Responsestream, System.Text.Encoding.UTF8); Ten             stringRetext =readstream.readtoend ().  ToString ();  One Readstream.close ();  A             returnRetext;  -    -}

. net/c# HttpWebRequest Transmit and receive parameters

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.