Learn Together rest (12.1) -- use rest in C #

Source: Internet
Author: User

Original address: http://rest.elkstein.org/

Learn rest: a tutorial

Send an http get request
The two main classes are in system.net.HttpwebrequestAndHttpwebresponse.

The following method sends a request and returns a long string:

static string HttpGet(string url) {  HttpWebRequest req = WebRequest.Create(url)                       as HttpWebRequest;  string result = null;  using (HttpWebResponse resp = req.GetResponse()                                as HttpWebResponse)  {    StreamReader reader =        new StreamReader(resp.GetResponseStream());    result = reader.ReadToEnd();  }  return result;}

Remember, if the URL contains parameters, it must be properly encoded (for example, the space is % 20, and so on ).The system. Web namespace is calledHttputilityClass, there is a static methodUrlencode for such Encoding.

Send an http post request
The URL in the POST request also needs to be encoded-in addition to form encoding, the method is as follows:

static string HttpPost(string url,     string[] paramName, string[] paramVal){  HttpWebRequest req = WebRequest.Create(new Uri(url))                        as HttpWebRequest;  req.Method = "POST";    req.ContentType = "application/x-www-form-urlencoded";  // Build a string with all the params, properly encoded.  // We assume that the arrays paramName and paramVal are  // of equal length:  StringBuilder paramz = new StringBuilder();  for (int i = 0; i < paramName.Length; i++) {    paramz.append(paramName[i]);    paramz.append("=");    paramz.append(HttpUtility.UrlEncode(paramVal[i]));    paramz.append("&");  }  // Encode the parameters as form data:  byte[] formData =      UTF8Encoding.UTF8.GetBytes(paramz.toString());  req.contentLength = formData.Length;  // Send the request:  using (Stream post = req.GetRequestStream())    {      post.Write(formData, 0, formData.Length);    }  // Pick up the response:  string result = null;  using (HttpWebResponse resp = req.GetResponse()                                as HttpWebResponse)    {      StreamReader reader =         new StreamReader(resp.GetResponseStream());    result = reader.ReadToEnd();  }  return result;}

For more examples, refer to Yahoo! This page in the Development Network.

 

Bydr. M. elkstein
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.