通过HttpClient来调用Web Api接口

回到目录

HttpClient是一个被封装好的类,主要用于Http的通讯,它在.net,java,oc中都有被实现,当然,我只会.net,所以,只讲.net中的HttpClient去调用Web Api的方法,基于api项目的特殊性,它需要有一个完全安全的环境,所以,你的api控制器看起来有点特别,只有5个方法,而且都是标准的http方法,我觉得这种设计很不错,很清晰,而且为了实现安全性,它不支持使用传统的表单数据,取而代之的是FromBody参数,它指拿HttpRequestMessage里参数,而不是所有的Request数据,这是基于安全方面的考虑。

一 Api接口参数的标准性

Get方式,可以有多个重载,有多个参数

POST方式,只能有一个参数,并且用[FromBody]约束,如果有多个参数,需要以对象的方式进行传递

Put方式,只能有两个参数,其中一个是通过Request.QueryString方式进行传递的,作为要更新对象的主键,别一个是[FromBody]字段,也是一个字段,如果多个字段需要把它封装成对象

标准接口如图

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAiYAAAC5CAIAAACTA2OuAAAgAElEQVR4nO2dPW7zsLPuuR6nu+BSXJ5Sq0gT3wUIQQp3qVwaJ0jFxkBS2guIcWCoMJDO/zTBPb2oW1CihuRQovwhO36fH1w4DD9GEjUPZyjbogIAAABGQVzbAAAAAP8KgyVHf2Vinhda9xbeDvqQyyeRffHmHWG8WgoxE2IW7RMAAEDIMMnRWmVPmfL0hit0KnxlxkFTN229tpgJ8ZQprYtPSauJmRBLddxRcWbL/MBY2Gt8vM8in0NyAABgAMMkRy0ZJ8sWWrRWGRdhxFp1yMMl6Da+A0gOAAAMZYDkHJdSi1U4UXKcIGmeF1qbhnLuRE5VZxKM2qa/MtvEyIn8LNiBGjsdyaFm0/dO6NY0dwrPFMkBAMDtkyo5rBKkyEPxKY1Xtem1Plc+LMqx9Wk4FegBE5F4A9E6+pBLPn/I168ikkOzdrY+wiMAwD9LquSoZS0VvYUeVnK8JidGOcZxNwGN9e9tQ2ob6+VD462pYVs6ENsnLzmHXD45u1NE0lr1BQCAf4QkydGHXIYpNa6Qaesm1s4iOW5AQ0MKR3I6ohz+iOoeP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fTdnLLktMEH62rrTv82U67cke/r4s3mi7z++Ts7E52O31yo+/XzuZNKzlpdsaN5wMvZrmauiHMuMUToxzyrzKX7c1/IS/Tm+DtOKLmv0UuRRv5cPtSf0ZygpncLznNrcTfCBHJad7/vi783Z1I6HArk6FqL403K5rQ54goJzJtwib3KTlmWeGpS5GXIhKBkpPbLzmVG/92mdQpOd4txGSE/dgiyCM1CWXqi3lLnIxT0Gfzhg7au7/q9umP7vmI5lSk2skPRFMcdmXKNukbiNTskRzrjKvm3rOrwqZy1+YqTXGwKdmTvExn/iTliIidtZeMPe2dKjnkoViVuYtllaU8a3uS5HAzOUFymjnP3gik8EO9TbwkwWIV7hTGHh+4kcnQjCWklO36gCYDVZYY5ZClSteHBO55L8fbeavo3l1W5rKVHK+mu0aoBLNwaHf5EhcUMckx/6LZIbYwvm3jr85oW/KgQbv8J9tITJ9ec24gJqRwcmvB6J47aFNeqXbyA9mc/kRt7eoy3N1lTYpMG34lTj5fkducQNX4TS/zYNMUdirSaeOkU1Qpgh1jdokTTsXQy1Te1nRn8B0ekZcNaRMszj5y88utgeQEyRR/G1nmyjl1jX+KWUi7PU5yKm4mxyUn6UaoyASbrr9pTBN7+j/cy7m1yVBxK4D2skdmSHiJ6d3BThsDvUGsYOiv5Ww2W3o/B5xQeFuSc61H1y9B+rOb1+3zuoS6Pv5nlc7+OdDUcd2UfWLwfS2SP7106iO5o0GfW3PKEx6SPr8xtzoZvE+DWsE4fM5ns9n880ArpxRCci4FJCcFm2GrhuTTzm+G+3m3sQYthbvavVlPXa+LSSwY469Ijglx2Ml2Ncm5vckQXs17S6xBcsbv8+rYJMlZPpd+vBkqu0Kg02aJB+Txb5lb/sIby4d6i8c3TGZpHG5wMugiz9xU6v1IDgAAgBsHkgMAAGAk8KugAAAAgAMkBwAAwEhAcgAAAIwEJAcAAMBIQHIAAACMBCQHAADASEByAAAAjMT/BzRoBBL0+L8/AAAAAElFTkSuQmCC" alt="" />

二 调用方,参数的标准性

在客户端进行接口调用时,我们以网页端为例,看一下网页端进行ajax跨域请求的代码

Get方式

通过HttpClient来调用Web Api接口
    $.ajax({
url: "http://localhost:52824/api/register",
type: "GET",
success: function (data) {
console.log("json:" + data);
}
});
通过HttpClient来调用Web Api接口

Post方式

通过HttpClient来调用Web Api接口
     $.ajax({
url: "http://localhost:52824/api/register",
type: "POST",
data: { '': '1' },//这里键名称必须为空,多个参数请传对象,api端参数名必须为value
success: function (data) {
console.log("post:" + data);
}
});
通过HttpClient来调用Web Api接口

三 在控制台中实现Get方式获取接口数据(只有异步实现)

通过HttpClient来调用Web Api接口
      /// <summary>
/// HttpClient实现Get请求
/// </summary>
static async void dooGet()
{
string url = "http://localhost:52824/api/register?id=1&leval=5";
//创建HttpClient(注意传入HttpClientHandler)
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; using (var http = new HttpClient(handler))
{
//await异步等待回应
var response = await http.GetAsync(url);
//确保HTTP成功状态值
response.EnsureSuccessStatusCode(); //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
通过HttpClient来调用Web Api接口

四 在控制台中实现Post方式提交数据(只有异步实现)

通过HttpClient来调用Web Api接口
     /// <summary>
/// HttpClient实现Post请求
/// </summary>
static async void dooPost()
{
string url = "http://localhost:52824/api/register";
var userId = "1";
//设置HttpClientHandler的AutomaticDecompression
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
//创建HttpClient(注意传入HttpClientHandler)
using (var http = new HttpClient(handler))
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{"", userId}//键名必须为空
}); //await异步等待回应 var response = await http.PostAsync(url, content);
//确保HTTP成功状态值
response.EnsureSuccessStatusCode();
//await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
Console.WriteLine(await response.Content.ReadAsStringAsync());
} }
通过HttpClient来调用Web Api接口

五 在控制台中实现Put方式提交数据(只有异步实现)

通过HttpClient来调用Web Api接口
        /// <summary>
/// HttpClient实现Put请求
/// </summary>
static async void dooPut()
{
string url = "http://localhost:52824/api/register?userid=" + userId;
var userId = "1";
//设置HttpClientHandler的AutomaticDecompression
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
//创建HttpClient(注意传入HttpClientHandler)
using (var http = new HttpClient(handler))
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{"", "数据"}//键名必须为空
}); //await异步等待回应 var response = await http.PutAsync(url, content);
//确保HTTP成功状态值
response.EnsureSuccessStatusCode();
//await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
Console.WriteLine(await response.Content.ReadAsStringAsync());
} }
通过HttpClient来调用Web Api接口

OK,到这里,我们的客户端如何去调用web api就讲完了,事实上,手机端,平板端也是相关的方式去调用的,它们也都有自己的HttpClient类,大同小异!

http://www.cnblogs.com/lori/p/4045413.html

上一篇:转-Fragment+FragmentTabHost组件(实现新浪微博底部菜单)


下一篇:hdu 1541 Stars