-
Notifications
You must be signed in to change notification settings - Fork 447
WebApiClient基础
本章节为了方便读者的理解,相关例子将使用HttpApiClient静态类来创建http接口的代理类,但在生产环境中,使用HttpApiFactory静态来创建http接口的代理类更合理,也是非常有必要的。
public interface IMyWebApi : IHttpApi
{
// GET http://www.mywebapi.com/webapi/user?account=laojiu
[HttpGet("http://www.mywebapi.com/webapi/user")]
ITask<string> GetUserByAccountAsync(string account);
}
var client = HttpApiClient.Create<IMyWebApi>();
var userStr = await client.GetUserByAccountAsync("laojiu");
[HttpHost("http://www.mywebapi.com/")]
public interface IMyWebApi : IHttpApi
{
// GET /webapi/user?account=laojiu
[HttpGet("webapi/user")]
ITask<string> GetUserByAccountAsync(string account);
}
如果接口IMyWebApi有多个方法且都指向同一服务器,可以将请求的域名抽出来放到HttpHost特性。
[HttpHost("http://www.mywebapi.com/")]
public interface IMyWebApi : IHttpApi
{
// GET /webapi/user?account=laojiu
[HttpGet("webapi/user")]
ITask<UserInfo> GetUserByAccountAsync(string account);
}
当方法的返回数据是UserInfo类型的json或xml文本,且响应的Content-Type为application/json或application/xml值时,方法的原有返回类型ITask就可以声明为ITask。
[HttpHost("http://www.mywebapi.com/")]
public interface IMyWebApi : IHttpApi
{
// GET /webapi/user?account=laojiu
[HttpGet("webapi/user")]
[JsonReturn] // 指明使用Json处理返回值为UserInfo类型
ITask<UserInfo> GetUserByAccountAsync(string account);
}
当方法的返回数据是UserInfo类型的json或xml文本,但响应的Content-Type可能不是期望的application/json或application/xml值时,就需要显式声明[JsonReturn]或[XmlReturn]特性。
无论是GET还是POST等哪种http请求方法,都遵循如下的URL格式:
{Scheme}://{UserName}:{Password}@{Host}:{Port}{Path}{Query}{Fragment}
例如:http://account:[email protected]/path1/?p1=abc#tag
某些接口方法将路径的一个分段语意化,比如GET http://www.webapiclient.com/{account}
,这里不同的{account}代表不同账号下的个人信息,使用{参数名}声明路径,在请求前会自动从参数(或参数模型的同名属性)取值替换。
public interface IMyWebApi : IHttpApi
{
// GET http://www.webapiclient.com/laojiu
[HttpGet("http://www.webapiclient.com/{account}"]
ITask<string> GetUserByAccountAsync(string account);
}
public interface IMyWebApi : IHttpApi
{
// GET {URL}
[HttpGet]
ITask<string> GetUserByAccountAsync([Url] string url);
// GET {URL}?account=laojiu
[HttpGet]
ITask<string> GetUserByAccountAsync([Url] string url, string account);
}
如果请求URL在运行时才确定,可以将请求URL作为一个参数,使用[Url]特性修饰这个参数并作为第一个参数。
// GET /webapi/user?account=laojiu&password=123456
[HttpGet("webapi/user")]
ITask<UserInfo> GetUserAsync(string account, string password);
public class LoginInfo
{
public string Account { get; set; }
public string Password { get; set; }
}
// GET /webapi/user?account=laojiu&password=123456
[HttpGet("webapi/user")]
ITask<UserInfo> GetUserAsync(LoginInfo loginInfo);
public class LoginInfo
{
public string Account { get; set; }
public string Password { get; set; }
}
// GET /webapi/user?account=laojiu&password=123456&role=admin
[HttpGet("webapi/user")]
ITask<UserInfo> GetUserAsync(LoginInfo loginInfo, string role);
// GET /webapi/user?account=laojiu&password=123456&role=admin
[HttpGet("webapi/user")]
ITask<UserInfo> GetUserAsync(
[PathQuery]LoginInfo loginInfo,
[PathQuery]string role);
对于没有任何特性修饰的每个参数,都默认被[PathQuery]修饰,表示做为请求路径或请求参数处理,[PathQuery]特性可以设置Encoding、IgnoreWhenNull和DateTimeFormat多个属性。
- 使用[XmlContent]修饰强类型模型参数,表示提交xml
- 使用[JsonContent]修饰强类型模型参数,表示提交json
// POST webapi/user
// Body user的json文本
[HttpPost("webapi/user")]
ITask<UserInfo> AddUserWithJsonAsync([JsonContent] UserInfo user);
// PUT webapi/user
// Body user的xml文本
[HttpPut("/webapi/user")]
ITask<UserInfo> UpdateUserWithXmlAsync([XmlContent] UserInfo user);
- 使用[FormContent]修饰强类型模型参数
- 使用[FormField]修饰简单类型参数
// POST webapi/user
// Body Account=laojiu&Password=123456
[HttpPost("webapi/user")]
ITask<UserInfo> UpdateUserWithFormAsync(
[FormContent] UserInfo user);
// POST webapi/user
// Body Account=laojiu&Password=123456&fieldX=xxx
[HttpPost("webapi/user")]
ITask<UserInfo> UpdateUserWithFormAsync(
[FormContent] UserInfo user,
[FormField] string fieldX);
- 使用[MulitpartContent]修饰强类型模型参数
- 使用[MulitpartText]修饰简单类型参数
- 使用MulitpartFile类型作为提交的文件
// POST webapi/user
[HttpPost("webapi/user")]
ITask<UserInfo> UpdateUserWithMulitpartAsync([MulitpartContent] UserInfo user);
// POST webapi/user
[HttpPost("/webapi/user")]
ITask<UserInfo> UpdateUserWithMulitpartAsync(
[MulitpartContent] UserInfo user,
[MulitpartText] string nickName,
MulitpartFile file);
// POST webapi/user
// Body Account=laojiu&Password=123456
[HttpPost("webapi/user")]
ITask<UserInfo> UpdateUserWithFormAsync(
FormUrlEncodedContent user);
// POST webapi/user
// Body Account=laojiu&Password=123456&age=18
[HttpPost("webapi/user")]
ITask<UserInfo> UpdateUserWithFormAsync(
FormUrlEncodedContent user,
[FormField] int age);
如果参数是类型是HttpContent类型的子类,如StringContent、ByteArrayContent、StreamContent、FormUrlEncodedContent等等,则可以直接做为参数,但是必须放在其它参数的前面:
public interface IMyWebApi : IHttpApi
{
// GET http://www.mywebapi.com/webapi/user?_name=laojiu
[HttpGet("http://www.mywebapi.com/webapi/user")]
ITask<string> GetUserByAccountAsync(
[AliasAs("_name")] string account);
}
参数account提交到服务器时使用的名称是_name。
public class UserInfo
{
[AliasAs("loginAccount")]
public string Account { get; set; }
public string Password { get; set; }
}
属性Account提交到服务器时使用的名称是loginAccount。
有些特性比如[Header],可以修饰于接口、方法和参数,使用不同的构造器和修饰于不同的地方产生的含义和结果是有点差别的:
- 修饰接口时,表示接口下的所有方法在请求前都会添加这个请求头;
- 修饰方法时,表示此方法在请求前添加这个请求头;
- 修饰参数时,表示参数的值将做为请求头的值,由调用者动态传入;
- 方法级比接口级优先级高;
- AllowMultiple为true时,方法级和接口级都生效;
- AllowMultiple为false时,方法级的生效,接口级的无效;