How to invoke the API

Source: Internet
Author: User

There is several ways to consume a RESTful API in C #:

    1. HttpWebRequest/ Response Class
    2. WebClientClass
    3. HttpClientClass
    4. RestSharpNuGet Package
    5. ServiceStackHttp Utils
Httpwebrequest/response Class
HttpWebRequest request = (HttpWebRequest) webrequest.create ("https://api.github.com/repos/restsharp/restsharp/ Releases "); request. Method = "GET"; request. useragent = "mozilla/5.0 (Windows NT 6.1; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/58.0.3029.110 safari/537.36 "; request. Automaticdecompression = Decompressionmethods.deflate | Decompressionmethods.gzip; HttpWebResponse response = (HttpWebResponse) request. GetResponse (); string content = String. Empty;using (Stream stream = response. GetResponseStream ()) {using (StreamReader sr = new StreamReader (stream)) {content = Sr. ReadToEnd ();}} var releases = Jarray.parse (content);

  

WebClient Class
var client = new WebClient (); client. Headers.add ("User-agent", "mozilla/5.0" (Windows NT 6.1; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/58.0.3029.110 safari/537.36 "); var response = client. Downloadstring ("https://api.github.com/repos/restsharp/restsharp/releases"); var releases = Jarray.parse (response) ;

  

HttpClient Class
using (var httpClient = new HttpClient ()) {HTTPCLIENT.DEFAULTREQUESTHEADERS.ADD ("user-agent", "mozilla/5.0"     ( Windows NT 6.1; Win64; x64) applewebkit/537.36 (khtml, like Gecko)      chrome/58.0.3029.110 safari/537.36 "); var response = Httpclient.getstringasync (new Uri (URL)). Result;var releases = Jarray.parse (response);}

  

Restsharp

RestSharp Is the opensource alternative to standard. NET libraries and one of the coolest. NET libraries out there. It's available as a NuGet package, and there be a few reasons why you should consider trying it out.

Like HttpClient , was RestSharp  a modern and comprehensive library, easy and pleasant to use, while still have support for older ver Sions of the. NET Framework. It has inbuilt authentication and serialization/deserialization mechanisms but allows you to override them with your Custo M ones. It is available across platforms and supports OAUTH1, OAuth2, Basic, NTLM and parameter-based authentication. It can also work synchronously or asynchronously. There is a lot of the library, but these is some of the great benefits it offers. For the detailed information on usage and capabilities of RESTSHARP, you can visit the Restsharp page on GitHub.

Now let's try to get a list of Restsharp releases using Restsharp.

var client = new Restclient (URL); irestresponse response = client. Execute (New Restrequest ()); var releases = Jarray.parse (response. Content);

  

ServiceStack Http Utils
var response = URL. Getjsonfromurl (Requestfilter:webreq =>{webreq.useragent = "mozilla/5.0" (Windows NT 6.1; Win64; x64)                         applewebkit/537.36 (khtml, like Gecko) chrome/58.0.3029.110 safari/537.36 ";}"; var releases = Jarray.parse (response);

  

How to invoke the API

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.