Skip to content

一款基于HttpClient封装,只需要定义c#接口并修饰相关特性,即可异步调用远程http接口的客户端库

License

Notifications You must be signed in to change notification settings

weinre/WebApiClient

 
 

Repository files navigation

PM> install-package Laojiu.WebApiClient

2. WebApiClient是什么

2.1 接口的声明

[HttpHost("http://www.webapiclient.com")] 
public interface MyWebApi : IDisposable
{
    // GET webapi/user?account=laojiu
    // Return 原始string内容
    [HttpGet("/webapi/user")]
    ITask<string> GetUserByAccountAsync(string account);

    // POST webapi/user  
    // Body Account=laojiu&password=123456
    // Return json或xml内容
    [HttpPost("/webapi/user")]
    ITask<UserInfo> UpdateUserWithFormAsync([FormContent] UserInfo user);
}

public class UserInfo
{
    public string Account { get; set; }

    [AliasAs("password")]
    public string Password { get; set; }

    [IgnoreSerialized]
    public string Email { get; set; }
}

2.2 接口的调用

static async Task TestAsync()
{
    var myWebApi = HttpApiClient.Create<MyWebApi>();
    var user = new UserInfo { Account = "laojiu", Password = "123456" }; 
    var user1 = await myWebApi.GetUserByAccountAsync("laojiu");
    var user2 = await myWebApi.UpdateUserWithFormAsync(user);
}

3. 详细文档

4. 怎么联系我

About

一款基于HttpClient封装,只需要定义c#接口并修饰相关特性,即可异步调用远程http接口的客户端库

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%