.NET Core – AgileHttp

做了这么多年一线开发,经常跟 Http 打交道。比如调用三方的 Webservice,比如集成微信支付的时候服务端发起 Prepay 支付。

 

特别是现在分布式、微服务大行其道,服务间通讯都离不开 http 调用。

 

多年前也造过几个 http client 的小轮子。这次使用C#强大的扩展方法进行了重构,使代码看起来有那么一点流式编程的风格,再配合 dynamic 有点写JavaScript的赶脚呢。

 

今天拿出来分享给大家,为.NET  Core 的生态尽一点绵薄之力。

 

Github: https://github.com/kklldog/AgileHttp 欢迎 star 。

安装

 

Install-Package AgileHttp

示例

 

使用HTTP.Send 方法

 

使用HTTP.Send / HTTP.SendAsync 方法可以直接发送一个请求

HTTP.Send("http://www.baidu.com") // 默认为 Get 方法
HTTP.Send("http://www.baidu.com", "POST")
HTTP.Send("http://www.baidu.com", "POST", new { name = "mjzhou" })
HTTP.Send("http://www.baidu.com", "POST", new { name = "mjzhou" }, new RequestOptions { ContentType = "application/json" })

ResponseInfo response = HTTP.Send("http://localhost:5000/api/user/1");
string content = response.GetResponseContent(); //获取 http 响应返回值的文本内容

 

HTTP.SendAsync 方法是 HTTP.Send 方法的异步版本

使用HttpClient

 

如果不喜欢手写”GET”,”POST”,”PUT”等 HTTP 方法,可以是使用HttpClient类。HttpClient类内置了 GET,POST,PUT,DELETE,OPTIONS 几个常用的方法。

var client = new HttpClient("http://www.baidu.com");
client.Get();//使用HttpClient发送 Get 请求


var client = new HttpClient("http://www.baidu.com");
client.Config(new RequestOptions { ContentType = "application/json" });
client.Post(new { name = "mjzhou" }); //使用 HttpClient 发送 Post 请求

ResponseInfo response = new HttpClient("http://localhost:5000/api/user/1").Get();
string content = response.GetResponseContent(); //获取 http 响应返回值的文本内容

User user1 = new HttpClient("http://localhost:5000/api/user/1").Get<User>(); //泛型方法可以直接反序列化成对象

 

Get,Post 等方法都有异步版本 GetAsync,PostAsync

使用扩展方法

 

C#强大的扩展方法可以让写代码行云流水。AgileHttp 提供了几个扩展方法,让使用更人性化。

var result =
"http://localhost:5000/api/user"
    .AppendQueryString("name", "kklldog")
    .AsHttpClient()
    .Get()
    .GetResponseContent();

var user =
"http://localhost:5000/api/user"
    .AppendQueryString("name", "kklldog")
    .AsHttpClient()
    .Get<User>();

 

1、String.AppendQueryString

给一个字符串添加查询参数

"http://localhost:5000/api/user".AppendQueryString("name", "mjzhou") // 返回结果为"http://localhost:5000/api/user?name=mjzhou"

 

2、String.AppendQueryStrings

 

给一个字符串添加多个查询参数

var qs = new Dictionary<string, object>();
qs.Add("a", "1");
qs.Add("b", "2");
"http://localhost:5000/api/user".AppendQueryStrings(qs)
// 返回结果为"http://localhost:5000/api/user?a=1&b=2"

 

3、String.AsHttp

 

以当前字符串为 URL 创建一个 HttpRequest

"http://www.baidu.com".AsHttp().Send();
//默认为 Get

"http://localhost:5000/api/user".AsHttp("POST",
new { name = "mjzhou" }).Send();

 

4、String.AsHttpClient

 

以当前字符串为 URL 创建一个 HttpClient

"http://www.baidu.com".AsHttpClient().Get();
"http://localhost:5000/api/user".AsHttpClient().Post(new { name = "mjzhou" });

 

5、ResponseInfo.Deserialize T

 

ResponseInfo 是请求结果的包装类,使用 Deserialize 方法可以直接反序列化成对象。如果没有配置 RequestOptions 则使用默认 SerializeProvider。

HTTP.Send("http://www.baidu.com").Deserialize<User>();

 

RequestOptions

 

使用 RequestOptions 可以对每个请求进行配置,比如设置 ContentType,设置 Headers,设置代理等等。

.NET Core - AgileHttp

 

关于序列化/反序列化

 

当你使用 Post,Put(不限于这 2 个方法)方法提交一个对象的时候 AgileHttp 会自动就行序列化。使用泛型 Get T, Post T 方法会自动进行反序列化。

 

默认使用 JsonSerializeProvider 来进行序列化及反序列化。JsonSerializeProvider 使用著名的 Newtonsoft.Json 实现了 ISerializeProvider接口,如果你喜欢你也可以自己实现自己的 Provider,比如实现一个 XMLSerializeProvider。

public interface ISerializeProvider
{
    T Deserialize<T>(string content);
string Serialize(object obj);
}

 

AgileHttp 提供 2 个地方来修改 SerializeProvider:

1、通过 RequestOptions 为单个 Http 请求配置序列化器

var xmlSerializeProvider = new xmlSerializeProvider();
var client = new HttpClient("http://www.baidu.com");
client.Config(new RequestOptions(xmlSerializeProvider));

 

2、通过 HTTP.SetDefaultSerializeProvider(ISerializeProvider provider)更改全局默认序列化器

var xmlSerializeProvider = new xmlSerializeProvider();
HTTP.SetDefaultSerializeProvider(xmlSerializeProvider);

注意:如果提交的 body 参数的类型为 String 或者 byte[]不会进行再次序列化。

 

转自:Agile.Zhou
cnblogs.com/kklldog/p/agilehttp.html

© 版权声明

☆ END ☆
喜欢就点个赞吧
点赞0 分享
图片正在生成中,请稍后...