通过HttpClient来调用Web Api接口
2017-09-27 11:54
337 查看
一 Api接口参数的标准性
Get方式,可以有多个重载,有多个参数
POST方式,只能有一个参数,并且用[FromBody]约束,如果有多个参数,需要以对象的方式进行传递
Put方式,只能有两个参数,其中一个是通过Request.QueryString方式进行传递的,作为要更新对象的主键,别一个是[FromBody]字段,也是一个字段,如果多个字段需要把它封装成对象
标准接口如图
二 调用方,参数的标准性
在客户端进行接口调用时,我们以网页端为例,看一下网页端进行ajax跨域请求的代码
Get方式
$.ajax({ url: "http://localhost:52824/api/register", type: "GET", success: function (data) { console.log("json:" + data); } });
Post方式
$.ajax({ url: "http://localhost:52824/api/register", type: "POST", data: { '': '1' },//这里键名称必须为空,多个参数请传对象,api端参数名必须为value success: function (data) { console.log("post:" + data); } });
三 在控制台中实现Get方式获取接口数据(只有异步实现)
/// <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()); } }
四 在控制台中实现Post方式提交数据(只有异步实现)
/// <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()); } }
五 在控制台中实现Put方式提交数据(只有异步实现)
/// <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()); } }
OK,到这里,我们的客户端如何去调用web api就讲完了,事实上,手机端,平板端也是相关的方式去调用的,它们也都有自己的HttpClient类,大同小异!
相关文章推荐
- c# 【MVC】WebApi通过HttpClient来调用Web Api接口
- WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递
- WebApi系列~通过HttpClient来调用Web Api接口
- WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递
- c# 【MVC】WebApi通过HttpClient来调用Web Api接口
- Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口
- WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递
- c# 【MVC】WebApi通过HttpClient来调用Web Api接口
- WebApi系列~通过HttpClient来调用Web Api接口
- 通过HttpClient调用接口时忽略SSL证书验证
- 使用httpclient实现http接口调用实例