WebApi系列通过HttpClient来调用WebApi接口.docx

上传人:小飞机 文档编号:3168754 上传时间:2023-03-11 格式:DOCX 页数:5 大小:37.65KB
返回 下载 相关 举报
WebApi系列通过HttpClient来调用WebApi接口.docx_第1页
第1页 / 共5页
WebApi系列通过HttpClient来调用WebApi接口.docx_第2页
第2页 / 共5页
WebApi系列通过HttpClient来调用WebApi接口.docx_第3页
第3页 / 共5页
WebApi系列通过HttpClient来调用WebApi接口.docx_第4页
第4页 / 共5页
WebApi系列通过HttpClient来调用WebApi接口.docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《WebApi系列通过HttpClient来调用WebApi接口.docx》由会员分享,可在线阅读,更多相关《WebApi系列通过HttpClient来调用WebApi接口.docx(5页珍藏版)》请在三一办公上搜索。

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

2、estMessage里参数,而不是所有的Request数据,这是基于安全方面的考虑。 一 Api接口参数的标准性 Get方式,可以有多个重载,有多个参数 POST方式,只能有一个参数,并且用FromBody约束,如果有多个参数,需要以对象的方式进行传递 Put方式,只能有两个参数,其中一个是通过Request.QueryString方式进行传递的,作为要更新对象的主键,别一个是FromBody字段,也是一个字段,如果多个字段需要把它封装成对象 标准接口如图 二 调用方,参数的标准性 在客户端进行接口调用时,我们以网页端为例,看一下网页端进行ajax跨域请求的代码 Get方式 $.ajax( u

3、rl: 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方式获取接口数据 / /

4、 HttpClient实现Get请求 / static async void dooGet string url = http:/localhost:52824/api/register?id=1&leval=5; /创建HttpClient var handler = new HttpClientHandler AutomaticDecompression = DecompressionMethods.GZip ; using (var http = new HttpClient(handler) /await异步等待回应 var response = await http.GetAsync

5、(url); /确保HTTP成功状态值 response.EnsureSuccessStatusCode; /await异步读取最后的JSON Console.WriteLine(await response.Content.ReadAsStringAsync); 四 在控制台中实现Post方式提交数据 / / HttpClient实现Post请求 / static async void dooPost string url = http:/localhost:52824/api/register; var userId = 1; /设置HttpClientHandler的AutomaticD

6、ecompression var handler = new HttpClientHandler AutomaticDecompression = DecompressionMethods.GZip ; /创建HttpClient using (var http = new HttpClient(handler) /使用FormUrlEncodedContent做HttpContent var content = new FormUrlEncodedContent(new Dictionary , userId/键名必须为空 ); /await异步等待回应 var response = awa

7、it http.PostAsync(url, content); /确保HTTP成功状态值 response.EnsureSuccessStatusCode; /await异步读取最后的JSON Console.WriteLine(await response.Content.ReadAsStringAsync); 五 在控制台中实现Put方式提交数据 / / HttpClient实现Put请求 / static async void dooPut string url = http:/localhost:52824/api/register?userid= + userId; var use

8、rId = 1; /设置HttpClientHandler的AutomaticDecompression var handler = new HttpClientHandler AutomaticDecompression = DecompressionMethods.GZip ; /创建HttpClient using (var http = new HttpClient(handler) /使用FormUrlEncodedContent做HttpContent var content = new FormUrlEncodedContent(new Dictionary , 数据/键名必须为空 ); /await异步等待回应 var response = await http.PutAsync(url, content); /确保HTTP成功状态值 response.EnsureSuccessStatusCode; /await异步读取最后的JSON Console.WriteLine(await response.Content.ReadAsStringAsync);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号