C # Send GET and POST requests,

Source: Internet
Author: User

C # Send GET and POST requests,
/// // HTTPGET

          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);          request.Method = "GET";          request.ContentType = "text/html;charset=UTF-8";          HttpWebResponse response = (HttpWebResponse)request.GetResponse();          Stream myResponseStream = response.GetResponseStream();          StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);          string retString = myStreamReader.ReadToEnd();          myStreamReader.Close();          myResponseStream.Close();

 

//// HTTPPOST
1 public static string HttpPost (string Url, string postDataStr) 2 {3 HttpWebRequest request = (HttpWebRequest) WebRequest. create (Url); 4 request. method = "POST"; 5 request. contentType = "application/x-www-form-urlencoded"; 6 request. contentLength = postDataStr. length; 7 StreamWriter writer = new StreamWriter (request. getRequestStream (), Encoding. ASCII); 8 writer. write (postDataStr); 9 writer. flush (); 10 HttpWebResponse response = (HttpWebResponse) request. getResponse (); 11 string encoding = response. contentEncoding; 12 if (encoding = null | encoding. length <1) {13 encoding = "UTF-8"; // default encoding 14} 15 StreamReader reader = new StreamReader (response. getResponseStream (), Encoding. getEncoding (encoding); 16 string retString = reader. readToEnd (); 17 return retString; 18}

 

Related Article

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.