如果是以前,想要用.net 发起一个Http请求真的非常复杂,需要写好几十行代码才行,现在好了,开源社区里面有几款不错的HTTP请求组件,这里我推你进来看看,这里的Demo我们就请求一个免费的API接口吧,我们先看看接口返回的数据




apipost 如何 请求 emqx api 接口_.net


然后创建一个该json适配的类,你也可以用VS里面自带的将JSON粘贴为类的功能,直接把根据json格式生成一个类,然后自己再稍加调整一下即可,这里我创建了两个类分别为ResultResponse和Result。

public class ResultResponse    {        public int code { get; set; }        public string message { get; set; }        public Result[] result { get; set; }    }      public class Result    {        public string sid { get; set; }        public string text { get; set; }        public string type { get; set; }        public string thumbnail { get; set; }        public string video { get; set; }        public object images { get; set; }        public string up { get; set; }        public string down { get; set; }        public string forward { get; set; }        public string comment { get; set; }        public string uid { get; set; }        public string name { get; set; }        public string header { get; set; }        public string top_comments_content { get; set; }        public string top_comments_voiceuri { get; set; }        public string top_comments_uid { get; set; }        public string top_comments_name { get; set; }        public string top_comments_header { get; set; }        public string passtime { get; set; }    }

因为我这个类是自动生成的,所以命名风格有点怪,这里就先忽略,但是实际应用中一定要注意这个问题。好了下面开始实践各个请求库了

refit


apipost 如何 请求 emqx api 接口_ide_02


直接通过Nuget即可安装


apipost 如何 请求 emqx api 接口_net api get 带+时间参数_03


这里我们新建一个名为IWebApi的接口:

public interface IWebApi    {        [Get("/getJoke?page={page}&count={count}&type={video}")]        Task GetJoke(int page,int count,string video);    }

这里的Get是refit的特性之一,里面的字符串即为请求路径和参数

现在,我们就去调用这个接口

[HttpGet("joke")]        public async Task GetJoke()        {            var webApi = RestService.For("https://api.apiopen.top/");            return await webApi.GetJoke(1,10, "video");        }

就这样简单的使用就可以获取我们接口的信息了


apipost 如何 请求 emqx api 接口_net api get 带+时间参数_04


refit为我们提供了很多特性,如果在请求时需要加Header,那么可以使用Headers这个特性。

EasyHttp

这个开源库已经很久没有更新了


apipost 如何 请求 emqx api 接口_net api get 带+时间参数_05


由于我演示是用的.net core 3.1,EasyHttp不支持Core,所以这里就不演示了,我就在Github搬一些案例过来吧

var http = new HttpClient();    http.Request.Accept = HttpContentTypes.ApplicationJson;    var response = http.Get("url");    var customer = response.DynamicBody;

如果是.net framework是的同学,可以使用一下。

RestSharp

这个库的热度还是毕竟高,已经达到了7.5k star


apipost 如何 请求 emqx api 接口_.net_06


这里我们就先省略Nuget安装,直接到示例编码

[HttpGet("joke")]        public async Task GetJoke()        {            var client = new RestClient("https://api.apiopen.top");            var request = new RestRequest("/getJoke?page=1&count=2&type=video", Method.GET);            IRestResponse rest= await client.ExecuteAsync(request);            return rest.Content;        }

这里只是一个简单的调用,它也提供了比较全面的工具方法,各位可以去官网了解一下

Flurl.Http

这个开源类库使用起来也是非常方便的,它扩展了字符串方法,在Nuget中安装Flurl.Http


apipost 如何 请求 emqx api 接口_字符串_07


然后一句代码即可发起HTTP请求并序列化成对象

[HttpGet("joke")]        public async Task GetJoke()        {          return await  "https://api.apiopen.top/getJoke?page=1&count=2&type=video".GetJsonAsync();        }

好了,这里只是简单的分享4款开源的http请求组件,使用的示例也是非常简单,并没有对这几个组件进行对比分析,你们在使用之前请先自行实践对比,进行最优选择。