C# consume RestApi

本文介绍了四种不同的HTTP请求处理方法:使用RestSharp库进行RESTful API调用,利用HttpWebRequest进行GET请求,通过HttpClient发送字符串请求,以及采用ServiceStack库获取JSON数据。每种方法都附带了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.RestSharp.

Nuget install RestSharp,Newtonsoft.Json.

using System;
using RestSharp;
using Newtonsoft.Json.Linq;

namespace DBDll
{
    public class RestSharpApi
    {
        public static void GetWebResonse(string baseUrl = "https://api.github.com/repos/restsharp/restsharp/releases")
        {            
            var client = new RestClient(baseUrl);
            IRestResponse response = client.Execute(new RestRequest());
            //return the formatted json string from a clumsy json string.
            var releases = JArray.Parse(response.Content);
            Console.WriteLine(releases);
        }
    }
}

2.HttpWebRequest

using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;

namespace DBDll
{
    public class HttpWebRequestDemo
    {
        public static void HttpWebRequestShow(string baseUrl = "https://api.github.com/repos/restsharp/restsharp/releases")
        {
            var httpRequest = (HttpWebRequest)WebRequest.Create(baseUrl);
            httpRequest.Method = "GET";
            httpRequest.UserAgent = "Mozilla / 5.0(Windows NT 6.1; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 58.0.3029.110 Safari / 537.36";
            httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            string content = string.Empty;
            using(var responseStream=httpResponse.GetResponseStream())
            {
                using(var sr=new StreamReader(responseStream))
                {
                    content = sr.ReadToEnd();
                }
            }
            Console.WriteLine(content);
        }
    }
}

3.HttpClient

using System;
using System.IO;
using System.Net.Http;

namespace DBDll
{
    public class HttpClientDemo
    {
        public static void HttpClientShow(string url)
        {
            HttpClient httpClient = new HttpClient();
            var response = httpClient.GetStringAsync(url);            
            Console.WriteLine(response.Result);
            string textFile = Directory.GetCurrentDirectory() + "//" + "web.txt";
            using(StreamWriter webWriter=new StreamWriter(textFile,true))
            {
                webWriter.WriteLine(response.Result+"\n");
            }
            
        }        
    }
}

 4.ServiceStack.

Install ServiceStack in Nuget.

using System;
using ServiceStack;

namespace DBDll
{
    public class ServiceStackDemo
    {
        public static void ServiceStackShow(string url)
        {
            var response= url.GetJsonFromUrl();
            Console.WriteLine(response);
        }
    }
}

 

转载于:https://www.cnblogs.com/Fred1987/p/11434256.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值