首页 技术 正文
技术 2022年11月12日
0 收藏 840 点赞 3,561 浏览 1949 个字

HttpClient是一个封装好的类,它在很多语言中都有被实现,现在HttpClient最新的版本是4.5。

它支持所有的http方法,自动转向,https协议,代理服务器。

一.Api接口参数标准化。

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

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

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

二.调用方

我们看一下最基本网页中ajax请求(Get | Set)

$.ajax({
url: "http://localhost:xxx/api/register",
type: "GET",
success: function (data) {
console.log("json:" + data);
}
});
$.ajax({
url: "http://localhost:xxx/api/register",
type: "Post",
data: {'':''}
success: function (data) {
console.log("json:" + data);
}
});

三.通过控制台实现Get&&Post请求

//Get:
static async void ClientGet()
{
string url = "http://localhost:xxx/api/register";
var handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip
};
using ( var http = new HttpClient( handler ) )
{
var response = await http.GetAsync( url ); response.EnsureSuccessStatusCode();
await response.Content.ReadAsStringAsync();
}
}
/*
* / <summary>
* / HttpClient实现Post请求
* / </summary>
*/
static async void dooPost()
{
string url = "http://localhost:52824/api/register";
var userId = "";
/* 设置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() );
}
}

无论是web,手机端,都是有自己的WebClient,都是一样的,语言也是,那如果说你的Api是一个的参数是一个对象类型的,这还是大同小异的,比如ajax的调用方式直接把data变成一个data对象,那我们C#的这个参数应该是个什么呢,这个时候我们应该用到FormUrlEncodedContent,使用这个类型去做httpcontext.

  var content = new FormUrlEncodedContent(new Dictionary<string, string>()
  { {"Id","6"},
{"Name","zara"},
{"Info", "zzh"}
 });

 那最后呢也没有什么区别了,只不过在异步去请求的时候去更换PostAsync , GetAsync , PutAsync 。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298