当前位置: 移动技术网 > IT编程>开发语言>.net > .Net Core - AgileHttp

.Net Core - AgileHttp

2020年01月13日  | 移动技术网IT编程  | 我要评论

2020年新年将至,先预祝.net core越来越好。
做了这么多年一线开发,经常跟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"
  1. 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"
  1. string.ashttp
    以当前字符串为url创建一个httprequest
"http://www.baidu.com".ashttp().send(); //默认为get
"http://localhost:5000/api/user".ashttp("post", new { name = "mjzhou" }).send();
  1. string.ashttpclient
    以当前字符串为url创建一个httpclient
"http://www.baidu.com".ashttpclient().get();
"http://localhost:5000/api/user".ashttpclient().post(new { name = "mjzhou" });
  1. responseinfo.deserialize t
    responseinfo是请求结果的包装类,使用deserialize方法可以直接反序列化成对象。如果没有配置requestoptions则使用默认serializeprovider。
http.send("http://www.baidu.com").deserialize<user>();

requestoptions

使用requestoptions可以对每个请求进行配置,比如设置contenttype,设置headers,设置代理等等。

属性 说明
serializeprovider 获取序列化器
encoding 获取编码方式
headers 获取或设置httpheaders
contenttype 获取或设置http contenttype属性
host 获取或设置http host属性
connection 获取或设置http connection属性
useragent 获取或设置http useragent属性
accept 获取或设置http accept属性
referer 获取或设置http referer属性
certificate 获取或设置x509证书信息
proxy 获取或设置代理信息

关于序列化/反序列化

当你使用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));
  1. 通过http.setdefaultserializeprovider(iserializeprovider provider)更改全局默认序列化器
var xmlserializeprovider = new xmlserializeprovider();
http.setdefaultserializeprovider(xmlserializeprovider);

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网