做网站那家比较好,seo入门教程视频,如何为企业做网站,网站建设面包屑导航条# 库简介WebApiClient是开源在github上的一个httpClient客户端库#xff0c;内部基于HttpClient开发#xff0c;是一个只需要定义c#接口(interface)#xff0c;并打上相关特性#xff0c;即可异步调用http-api的框架 #xff0c;支持.net framework4.5、netcoreapp2.0和ne… # 库简介WebApiClient是开源在github上的一个httpClient客户端库内部基于HttpClient开发是一个只需要定义c#接口(interface)并打上相关特性即可异步调用http-api的框架 支持.net framework4.5、netcoreapp2.0和netstandard2.0。本文将详细地讲解如何使用WebApiClient进行http接口的调用给.net开发者提供一种有别于传统的http接口调用编程方式。1. 设计一个Get请求接口1.1 最简单的Get请求public interface MyWebApi : IDisposable{ // GET http://www.mywebapi.com/webapi/user?accountlaojiu[HttpGet(http://www.mywebapi.com/webapi/user)] ITaskstring GetUserByAccountAsync(string account);
} var myWebApi HttpApiClient.CreateMyWebApi(); var userStr await myWebApi.GetUserByAccountAsync(laojiu);
myWebApi.Dispose();1.2 使用[HttpHost]特性如果你有多个接口而且都指向对一服务器可以将请求的域名抽出来放到HttpHost特性接口的代码如下[HttpHost(http://www.mywebapi.com)]public interface MyWebApi : IDisposable{ // GET /webapi/user?accountlaojiu[HttpGet(/webapi/user)] ITaskstring GetUserByAccountAsync(string account);
}1.3 使用强类型返回值处理xml或json如果接口的返回内容是xml或json你希望将它自动映射为强类型的模型需要给接口打上对应的[XmlReturn]或[JsonResult]。实际上有一个[AutoReturn]它会根据回复头标识自动选择不同的转换器转换为TResult类型的结果默认的每个接口都使用了[AutoReturn]除非给接口显性地给方法配置了[XmlReturn]或[JsonResult]。如果以上返回的userStr是UserInfo类型的xml或json文本那么强类型的代码声明如下[HttpHost(http://www.mywebapi.com)]public interface MyWebApi : IDisposable{ // GET /webapi/user?accountlaojiu[HttpGet(/webapi/user)] ITaskUserInfo GetUserByAccountAsync(string account);
}[HttpHost(http://www.mywebapi.com)]
[JsonReturn] // 指明使用Json处理返回值为UserInfo类型public interface MyWebApi : IDisposable{ // GET /webapi/user?accountlaojiu[HttpGet(/webapi/user)] ITaskUserInfo GetUserByAccountAsync(string account);
}2.请求URL的多个参数2.1 参数平铺你可以将多个参数一一设计为接口的参数类似于// GET /webapi/user?accountlaojiupassword123456[HttpGet(/webapi/user)]ITaskUserInfo GetUserAsync(string account, string password);2.2 参数合并到模型也可以将所有参数合到一个简单的多属性模型对象public class MyParameters{ public string Account { get; set; } public string Password { get; set; }
}// GET /webapi/user?accountlaojiupassword123456[HttpGet(/webapi/user)]ITaskUserInfo GetUserAsync(MyParameters parameters);2.3 模型简单参数混合在一些场景中除了提交多属性模型对象之外可能还需要一个简单类型的额外参数你可以如下编写接口:// GET /webapi/user?accountlaojiupassword123456birthDay2010-01-01 01:01:01[HttpGet(/webapi/user)]ITaskUserInfo GetUserAsync(MyParameters parametersDateTime birthDay);上面这里你可能会遇到一个问题birthDay会简单的ToString()值做为参数值如果你希望只需要日期而不包含时间你可以给birthDay指定格式// GET /webapi/user?accountlaojiupassword123456birthDay2010-01-01[HttpGet(/webapi/user)]ITaskUserInfo GetUserAsync(MyParameters parameters[PathQuery(yyyy-MM-dd)] DateTime birthDay);实际上对于没有任何特性修饰的每个参数都默认被[PathQuery]修饰表示做为请求路径或请求参数处理[PathQuery]的构造器重载方法可以指定日期时间格式。3.设计一个Post请求接口3.1使用x-www-form-urlencoded提交请求// POST webapi/user // Body AccountlaojiuPassword123456[HttpPost(/webapi/user)]ITaskUserInfo UpdateUserWithFormAsync([FormContent] UserInfo user);设计风格和Get请求是差不多的你应该发现接口参数被[FormContent]修饰了[FormContent]的作用是将模型参数user以key1value1key2value2的方式写入到请求内容中。如果你还需要提供一个额外的简单类型参数需要使用[FormField]修饰这个参数可以这样设计接口// POST webapi/user // Body AccountlaojiuPassword123456fieldXxxx[HttpPost(/webapi/user)]ITaskUserInfo UpdateUserWithFormAsync([FormContent] UserInfo user, [FormField] string fieldX);3.2使用multipart/form-data提交请求// POST webapi/user [HttpPost(/webapi/user)]ITaskUserInfo UpdateUserWithMulitpartAsync([MulitpartContent] UserInfo user);// POST webapi/user [HttpPost(/webapi/user)]ITaskUserInfo UpdateUserWithMulitpartAsync([MulitpartContent] UserInfo user, [MulitpartText] string nickName,MulitpartFile file);需要了解的是[MulitpartText]表示是一个文本项而MulitpartFile表示一个文件项MulitpartFile实现了IApiParameterable接口它不需要任何特性的修饰它能提供自我解释和处理。3.3提交Json或Xml文本对于json和xml只能一次性提交一个参数不支持额外参数之说// POST webapi/user // Body user的json文本[HttpPost(/webapi/user)]ITaskUserInfo UpdateUserWithJsonAsync([JsonContent] UserInfo user);// POST webapi/user // Body user的xml文本[HttpPost(/webapi/user)]ITaskUserInfo UpdateUserWithXmlAsync([XmlContent] UserInfo user);如果你的UserInfo有DateTime类型的属性你可以使用[JsonContent(时间格式)]来修饰接口参数否则时间格式使用HttpApiConfig的DateTimeFormate。3.4 提交原始的HttpContent// POST webapi/user // Body AccountlaojiuPassword123456[HttpPost(/webapi/user)]ITaskUserInfo UpdateUserWithFormAsync(FormUrlEncodedContent user);// POST webapi/user // Body AccountlaojiuPassword123456age18[HttpPost(/webapi/user)]ITaskUserInfo UpdateUserWithFormAsync([HttpContent] FormUrlEncodedContent user,[FormField] int age);默认的所有System.Net.Http.HttpContent类型的参数都会被[HttpContent]特性修饰而且可以与表单字段特性等混合使用。值得说明的话传统的System.Net.Http.HttpContent类型参数必须放到其它表单字段参数的前面。4. 动态指定请求的域名或Url4.1 域名动态而相对路径固定以上的例子请求的根路径都是硬编码而在不少场景中是放在配置文件中的可以在创建接口实例时创建配置项var config new HttpApiConfig
{ // 请求的域名会覆盖[HttpHost]特性HttpHost new Uri(http://www.webapiclient.com),
};var myWebApi HttpApiClient.CreateMyWebApi(config);var userStr await myWebApi.GetUserByAccountAsync(laojiu);
myWebApi.Dispose();4.2 每个请求接口的URL路径都是动态的有时多个接口方法的全部URL都是运行时才确定的这时需要给每个接口做如下的调整注意[Url]特性表示参数是请求的URL要求必须放在第一个参数。public interface MyWebApi : IDisposable{ // GET {URL}?accountlaojiu[HttpGet] ITaskstring GetUserByAccountAsync([Url] string url, string account);
}4.3 相对路径某个分段动态有时有些接口会将某个参数做路径的一个分段比如GET http://www.webapiclient.com/{account}这里的{account}是动态的获取哪个账号的资料就填写哪个账号可以如下设计接口public interface MyWebApi : IDisposable{ // GET http://www.webapiclient.com/laojiu[HttpGet(http://www.webapiclient.com/{account}] ITaskstring GetUserByAccountAsync(string account);
}5.参数别名或属性别名5.1 参数别名有些服务端接口要求的键名与你的编程风格不一致或者使用了特殊的键名为.net语言不允许的参数名你可以使用[AliasAs(name)]来给参数或模型的属性别名。public interface MyWebApi : IDisposable{ // GET http://www.mywebapi.com/webapi/user?_namelaojiu[HttpGet(http://www.mywebapi.com/webapi/user)] ITaskstring GetUserByAccountAsync([AliasAs(_name)] string account);
}5.2 模型的属性别名public class UserInfo{[AliasAs(loginAccount)] public string Account { get; set; } public string Password { get; set; }
}6.特性的范围和优先级6.1 特性的范围有些特性比如[Header]可以修饰于接口、方法和参数使用不同的构造器和修饰于不同的地方产生的含义和结果是有点差别的修饰接口时表示接口下的所有方法在请求前都会添加这个请求头修饰方法时表示此方法在语法前添加这个请求头修饰参数时表示参数的值将做为请求头的值由调用者动态传入6.2 特性的优先级有些特性比如[AutoReturn]和[JsonReturn]可以修饰于接口和方法但特性的AllowMultiple为false如果在接口级生明方法级[AutoReturn],在方法级上声明[JsonReturn]此方法实际生效的是[JsonReturn]再比如[Timeout]特性如果在接口级声明[Timeout(5000)]在方法级声明[Timeout(10000)]实际生效的是[Timeout(10000)]总结如下AllowMultiple为false的同一个特性方法级比接口级优先级高AllowMultiple为false的不同类型的[ReturnAttribute]方法级比接口级优先级高相关内容 自动类型安全的REST .NET标准库refitWebApi client 的面向切面编程原文地址:https://www.cnblogs.com/cgzl/p/8287588.html.NET社区新闻深度好文欢迎访问公众号文章汇总 http://www.csharpkit.com