WebApi调用
Lio 2021-03-30 WebApiRequest
# 前端JQuery调用
<script src="~/Scripts/jquery-3.4.1.js"></script>
<script>
$(function () {
var user = { UserId: "11", UserName: "xxx", UserEmail: "2222222@qq.com" };
var info = "this is muti model";
$("#btnGet1").on("click", function () {
//$.ajax({ url: "/api/users", type: "get", data: { "userName": "Superman" }, success: function (data) { alert(data); }, datatype: "json" });//指向接口,参数匹配的,大小写不区分
$.ajax({
url: "/api/users/GetUserByName", type: "get", data: { "username": "Superman" },
beforeSend: function (XHR) {
//发送ajax请求之前向http的head里面加入验证信息
XHR.setRequestHeader('Authorization', 'BasicAuth ' + ticket);
},
success: function (data) {
alert(data);
}, datatype: "json"
});
});
$("#btnGet2").on("click", function () {//单个参数
$.ajax({ url: "/api/users/GetUserByID", type: "get", data: { "id": $("#txtId").val() }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet3").on("click", function () {//两个参数
$.ajax({ url: "/api/users/GetUserByNameId", type: "get", data: { "userName": "Superman", "id": $("#txtId").val() }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet4").on("click", function () {//无参数
$.ajax({ url: "/api/users/Get", type: "get", data: "", success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet5").on("click", function () {//传递实体 json对象
$.ajax({ url: "/api/users/GetUserByModel", type: "get", data: user, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet6").on("click", function () {//传递实体 必须FromUri才能找到
$.ajax({ url: "/api/users/GetUserByModelUri", type: "get", data: user, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet7").on("click", function () {//传递实体,序列化后传递
$.ajax({ url: "/api/users/GetUserByModelSerialize", type: "get", data: { userString: JSON.stringify(user) }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet8").on("click", function () {//传递实体,序列化后传递
$.ajax({ url: "/api/users/GetUserByModelSerializeWithoutGet", type: "get", data: { userString: JSON.stringify(user) }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet9").on("click", function () {//传递实体,序列化后传递 405 Method Not Allowed 不带httpget需要用get开头
$.ajax({ url: "/api/users/NoGetUserByModelSerializeWithoutGet", type: "get", data: { userString: JSON.stringify(user) }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPost1").on("click", function () {//单个值传递,json数据不要key,这样后台才能获取
$.ajax({ url: "/api/users/RegisterNoKey", type: "post", data: { "": $("#txtId").val() }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPost2").on("click", function () {//key-value形式后台拿不到这个参数,但是可以直接访问
$.ajax({ url: "/api/users/Register", type: "post", data: { "id": $("#txtId").val() }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPost3").on("click", function () {//传递json格式的,后台可以用实体接收
$.ajax({ url: "/api/users/RegisterUser", type: "post", data: user, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPost4").on("click", function () {//传递json序列化后的格式,后台可以用实体接收,需要指定contentType
$.ajax({ url: "/api/users/RegisterUser", type: "post", data: JSON.stringify(user), success: function (data) { alert(data); }, datatype: "json", contentType: 'application/json', });
});
$("#btnPost5").on("click", function () {//JObject接收
$.ajax({ url: "/api/users/RegisterObject", type: "post", data: { "User": user, "Info": info }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPost6").on("click", function () {//Dynamic 失败了 要序列化就可以了
$.ajax({ url: "/api/users/RegisterObjectDynamic", type: "post", data: { "User": user, "Info": info }, success: function (data) { alert(data); }, datatype: "json", contentType: 'application/json' });
});
//也可以还是包装成一个对象
$("#btnPut1").on("click", function () {//单个值传递,json数据不要key,这样后台才能获取
$.ajax({ url: "/api/users/RegisterNoKeyPut", type: "put", data: { "": $("#txtId").val() }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPut2").on("click", function () {//key-value形式后台拿不到
$.ajax({ url: "/api/users/RegisterPut", type: "put", data: { "id": $("#txtId").val() }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPut3").on("click", function () {//传递json格式的,后台可以用实体接收
$.ajax({ url: "/api/users/RegisterUserPut", type: "put", data: user, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPut4").on("click", function () {//传递json序列化后的格式,后台可以用实体接收,需要指定contentType
$.ajax({ url: "/api/users/RegisterUserPut", type: "put", data: JSON.stringify(user), success: function (data) { alert(data); }, datatype: "json", contentType: 'application/json', });
});
$("#btnPut5").on("click", function () {//JObject接收
$.ajax({ url: "/api/users/RegisterObjectPut", type: "put", data: { "User": user, "Info": info }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPut6").on("click", function () {//Dynamic 失败了
$.ajax({ url: "/api/users/RegisterObjectDynamicPut", type: "put", data: { "User": user, "Info": info }, success: function (data) { alert(data); }, datatype: "json", contentType: 'application/json' });
});
//delete一样 type换成delete
//如果大家还有别的传递和获取 自动绑定好的方式,欢迎分享
$("#btnLogin").on("click", function () {
$.ajax({
url: "/api/users/Login",
type: "GET",
data: { "Account": $("#txtAccount").val(), "Password": $("#txtPassword").val() },
success: function (data) {
var result = JSON.parse(data);
if (result.Result) {
ticket = result.Ticket;
alert(result.Ticket);
}
else {
alert("failed");
}
}, datatype: "json"
});
});
var ticket = "";//登陆后放在某个html里面,ajax都得带上
//microsoft.aspnet.webapi.cors
jQuery.support.cors = true;
var location = "http://localhost:8888";
$("#btnGetCors1").on("click", function () {
$.ajax({ url: location + "/api/users/GetUserByID", type: "get", data: { "id": 1 }, success: function (data) { alert(data); }, datatype: "json" });
});
});
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 后端调用
WebApi全部遵循http协议,httpMethod,后端模拟http请求,有两张方式:HttpWebRequest和HttpClient
[TestClass]
public class WebapiTest
{
[TestMethod]
public void TestMethod()
{
this.AuthorizationDemo();
var result1 = this.GetClient();
var result2 = this.GetWebQuest();
var result3 = this.PostClient();
var result4 = this.PostWebQuest();
var result5 = this.PutClient();
var result6 = this.PutWebQuest();
var result7 = this.DeleteClient();//传值只能是url
var result8 = this.DeleteWebQuest();
//Console.WriteLine();
}
#region HttpClient Get
/// <summary>
/// HttpClient实现Get请求
/// </summary>
private string GetClient()
{
//string url = "http://localhost:8888/api/users/GetUserByName?username=superman";
//string url = "http://localhost:8888/api/users/GetUserByID?id=1";
//string url = "http://localhost:8888/api/users/GetUserByNameId?userName=Superman&id=1";
//string url = "http://localhost:8888/api/users/Get";
//string url = "http://localhost:8888/api/users/GetUserByModel?UserID=11&UserName=Eleven&UserEmail=57265177%40qq.com";
string url = "http://localhost:8888/api/users/GetUserByModelUri?UserID=11&UserName=Eleven&UserEmail=57265177%40qq.com";
//string url = "http://localhost:8888/api/users/GetUserByModelSerialize?userString=%7B%22UserID%22%3A%2211%22%2C%22UserName%22%3A%22Eleven%22%2C%22UserEmail%22%3A%2257265177%40qq.com%22%7D";
//string url = "http://localhost:8888/api/users/GetUserByModelSerializeWithoutGet?userString=%7B%22UserID%22%3A%2211%22%2C%22UserName%22%3A%22Eleven%22%2C%22UserEmail%22%3A%2257265177%40qq.com%22%7D";
//string url = "http://localhost:8888/api/users/NoGetUserByModelSerializeWithoutGet?userString=%7B%22UserID%22%3A%2211%22%2C%22UserName%22%3A%22Eleven%22%2C%22UserEmail%22%3A%2257265177%40qq.com%22%7D";
var handler = new HttpClientHandler();//{ AutomaticDecompression = DecompressionMethods.GZip };
using (var http = new HttpClient(handler))
{
var response = http.GetAsync(url).Result;//拿到异步结果
Console.WriteLine(response.StatusCode); //确保HTTP成功状态值
//await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
return response.Content.ReadAsStringAsync().Result;
}
}
#endregion
#region HttpWebRequest实现Get请求
/// <summary>
/// HttpWebRequest实现Get请求
/// </summary>
private string GetWebQuest()
{
//string url = "http://localhost:8888/api/users/GetUserByName?username=superman";
//string url = "http://localhost:8888/api/users/GetUserByID?id=1";
//string url = "http://localhost:8888/api/users/GetUserByNameId?userName=Superman&id=1";
//string url = "http://localhost:8888/api/users/Get";
//string url = "http://localhost:8888/api/users/GetUserByModel?UserID=11&UserName=Eleven&UserEmail=57265177%40qq.com";
//string url = "http://localhost:8888/api/users/GetUserByModelUri?UserID=11&UserName=Eleven&UserEmail=57265177%40qq.com";
string url = "http://localhost:8888/api/users/GetUserByModelSerialize?userString=%7B%22UserID%22%3A%2211%22%2C%22UserName%22%3A%22Eleven%22%2C%22UserEmail%22%3A%2257265177%40qq.com%22%7D";
//string url = "http://localhost:8888/api/users/GetUserByModelSerializeWithoutGet?userString=%7B%22UserID%22%3A%2211%22%2C%22UserName%22%3A%22Eleven%22%2C%22UserEmail%22%3A%2257265177%40qq.com%22%7D";
//string url = "http://localhost:8888/api/users/NoGetUserByModelSerializeWithoutGet?userString=%7B%22UserID%22%3A%2211%22%2C%22UserName%22%3A%22Eleven%22%2C%22UserEmail%22%3A%2257265177%40qq.com%22%7D";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 30 * 1000;
//request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
//request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
string result = "";
using (var res = request.GetResponse() as HttpWebResponse)
{
if (res.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
}
return result;
}
#endregion
#region HttpClient实现Post请求
/// <summary>
/// HttpClient实现Post请求
/// </summary>
private string PostClient()
{
//string url = "http://localhost:8888/api/users/register";
//Dictionary<string, string> dic = new Dictionary<string, string>()
//{
// {"","1" }
//};
//string url = "http://localhost:8888/api/users/RegisterNoKey";
//Dictionary<string, string> dic = new Dictionary<string, string>()
//{
// {"","1" }
//};
//string url = "http://localhost:8888/api/users/RegisterUser";
//Dictionary<string, string> dic = new Dictionary<string, string>()
//{
// {"UserID","11" },
// {"UserName","Eleven" },
// {"UserEmail","57265177@qq.com" },
//};
string url = "http://localhost:8888/api/users/RegisterObject";
Dictionary<string, string> dic = new Dictionary<string, string>()
{
{"User[UserID]","11" },
{"User[UserName]","Eleven" },
{"User[UserEmail]","57265177@qq.com" },
{"Info","this is muti model" }
};
HttpClientHandler handler = new HttpClientHandler();
using ( var http = new HttpClient(handler))
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(dic);
var response = http.PostAsync(url, content).Result;
Console.WriteLine(response.StatusCode); //确保HTTP成功状态值
return response.Content.ReadAsStringAsync().Result;
}
}
#endregion
#region HttpWebRequest实现post请求
/// <summary>
/// HttpWebRequest实现post请求
/// </summary>
private string PostWebQuest()
{
//string url = "http://localhost:8888/api/users/register";
//var postData = "1";
//string url = "http://localhost:8888/api/users/RegisterNoKey";
//var postData = "1";
var user = new
{
UserID = "11",
UserName = "Eleven",
UserEmail = "57265177@qq.com"
};
//string url = "http://localhost:8888/api/users/RegisterUser";
//var postData = JsonHelper.ObjectToString(user);
var userOther = new
{
User = user,
Info = "this is muti model"
};
string url = "http://localhost:8888/api/users/RegisterObject";
var postData = Newtonsoft.Json.JsonConvert.SerializeObject(userOther);
var request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Timeout = 30 * 1000;//设置30s的超时
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
request.ContentType = "application/json";
request.Method = "POST";
byte[] data = Encoding.UTF8.GetBytes(postData);
request.ContentLength = data.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(data, 0, data.Length);
postStream.Close();
string result = "";
using (var res = request.GetResponse() as HttpWebResponse)
{
if (res.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
}
return result;
}
#endregion
#region HttpClient实现Put请求
/// <summary>
/// HttpClient实现Put请求
/// </summary>
private string PutClient()
{
string url = "http://localhost:8888/api/users/RegisterPut";
Dictionary<string, string> dic = new Dictionary<string, string>()
{
{"","1" }
};
//string url = "http://localhost:8888/api/users/RegisterNoKeyPut";
//Dictionary<string, string> dic = new Dictionary<string, string>()
//{
// {"","1" }
//};
//string url = "http://localhost:8888/api/users/RegisterUserPut";
//Dictionary<string, string> dic = new Dictionary<string, string>()
//{
// {"UserID","11" },
// {"UserName","Eleven" },
// {"UserEmail","57265177@qq.com" },
//};
//string url = "http://localhost:8888/api/users/RegisterObjectPut";
//Dictionary<string, string> dic = new Dictionary<string, string>()
//{
// {"User[UserID]","11" },
// {"User[UserName]","Eleven" },
// {"User[UserEmail]","57265177@qq.com" },
// {"Info","this is muti model" }
//};
HttpClientHandler handler = new HttpClientHandler();
using (var http = new HttpClient(handler))
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(dic);
var response = http.PutAsync(url, content).Result;
Console.WriteLine(response.StatusCode); //确保HTTP成功状态值
return response.Content.ReadAsStringAsync().Result;
}
}
#endregion
#region HttpWebRequest实现put请求
/// <summary>
/// HttpWebRequest实现put请求
/// </summary>
private string PutWebQuest()
{
//string url = "http://localhost:8888/api/users/registerPut";
//var postData = "1";
//string url = "http://localhost:8888/api/users/RegisterNoKeyPut";
//var postData = "1";
var user = new
{
UserID = "11",
UserName = "Eleven",
UserEmail = "57265177@qq.com"
};
//string url = "http://localhost:8888/api/users/RegisterUserPut";
//var postData = JsonHelper.ObjectToString(user);
var userOther = new
{
User = user,
Info = "this is muti model"
};
string url = "http://localhost:8888/api/users/RegisterObjectPut";
var postData = Newtonsoft.Json.JsonConvert.SerializeObject(userOther);
var request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Timeout = 30 * 1000;//设置30s的超时
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
request.ContentType = "application/json";
request.Method = "PUT";
byte[] data = Encoding.UTF8.GetBytes(postData);
request.ContentLength = data.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(data, 0, data.Length);
postStream.Close();
string result = "";
using (var res = request.GetResponse() as HttpWebResponse)
{
if (res.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
}
return result;
}
#endregion
#region HttpClient实现Delete请求
/// <summary>
/// HttpClient实现Put请求
/// 没放入数据
/// </summary>
private string DeleteClient()
{
string url = "http://localhost:8888/api/users/RegisterDelete/1";
Dictionary<string, string> dic = new Dictionary<string, string>()
{
{"","1" }
};
//string url = "http://localhost:8888/api/users/RegisterNoKeyDelete";
//Dictionary<string, string> dic = new Dictionary<string, string>()
//{
// {"","1" }
//};
//string url = "http://localhost:8888/api/users/RegisterUserDelete";
//Dictionary<string, string> dic = new Dictionary<string, string>()
//{
// {"UserID","11" },
// {"UserName","Eleven" },
// {"UserEmail","57265177@qq.com" },
//};
//string url = "http://localhost:8888/api/users/RegisterObjectDelete";
//Dictionary<string, string> dic = new Dictionary<string, string>()
//{
// {"User[UserID]","11" },
// {"User[UserName]","Eleven" },
// {"User[UserEmail]","57265177@qq.com" },
// {"Info","this is muti model" }
//};
HttpClientHandler handler = new HttpClientHandler();
using (var http = new HttpClient(handler))
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(dic);
var response = http.DeleteAsync(url).Result;
Console.WriteLine(response.StatusCode); //确保HTTP成功状态值
return response.Content.ReadAsStringAsync().Result;
}
}
#endregion
#region HttpWebRequest实现put请求
/// <summary>
/// HttpWebRequest实现put请求
/// </summary>
private string DeleteWebQuest()
{
//string url = "http://localhost:8888/api/users/registerDelete";
//var postData = "1";
//string url = "http://localhost:8888/api/users/RegisterNoKeyDelete";
//var postData = "1";
var user = new
{
UserID = "11",
UserName = "Eleven",
UserEmail = "57265177@qq.com"
};
//string url = "http://localhost:8888/api/users/RegisterUserDelete";
//var postData = JsonHelper.ObjectToString(user);
var userOther = new
{
User = user,
Info = "this is muti model"
};
string url = "http://localhost:8888/api/users/RegisterObjectDelete";
var postData = Newtonsoft.Json.JsonConvert.SerializeObject(userOther);
var request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Timeout = 30 * 1000;//设置30s的超时
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
request.ContentType = "application/json";
request.Method = "Delete";
byte[] data = Encoding.UTF8.GetBytes(postData);
request.ContentLength = data.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(data, 0, data.Length);
postStream.Close();
string result = "";
using (var res = request.GetResponse() as HttpWebResponse)
{
if (res.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
}
return result;
}
#endregion
#region 用户登陆 获取ticket后使用
private void AuthorizationDemo()
{
string ticket = "";
{
string loginUrl = "http://localhost:8888/api/users/Login?Account=Admin&Password=123456";
var handler = new HttpClientHandler();//{ AutomaticDecompression = DecompressionMethods.GZip };
using (var http = new HttpClient(handler))
{
var response = http.GetAsync(loginUrl).Result;//拿到异步结果
Console.WriteLine(response.StatusCode); //确保HTTP成功状态值
//await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
ticket = response.Content.ReadAsStringAsync().Result.Replace("\"{\\\"Result\\\":true,\\\"Ticket\\\":\\\"", "").Replace("\\\"}\"", "");
//ticket = JsonHelper.StringToObject<TicketModel>(response.Content.ReadAsStringAsync().Result).Ticket;
}
}
{
string url = "http://localhost:8888/api/users/GetUserByName?username=superman";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 30 * 1000;
request.Headers.Add(HttpRequestHeader.Authorization, "BasicAuth " + ticket);//头文件增加Authorization
//request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
//request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
string result = "";
using (var res = request.GetResponse() as HttpWebResponse)
{
if (res.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
}
}
{
string url = "http://localhost:8888/api/users/GetUserByName?username=superman";
var handler = new HttpClientHandler();
using (var http = new HttpClient(handler))
{
http.DefaultRequestHeaders.Add("Authorization", "BasicAuth " + ticket);//头文件增加Authorization
var response = http.GetAsync(url).Result;
Console.WriteLine(response.StatusCode);
string result = response.Content.ReadAsStringAsync().Result;
}
}
}
#endregion
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
using (var http = new HttpClient(handler)) 不太好 tcp其实并不能立即释放掉
如果是高并发的这样操作,会出现资源不够 积极拒绝
HttpClient内部有个连接池,各个请求是隔开的
要求HttpClient的实例化都从这里来 全局只要一个实例,不要using
各个请求是隔开的,可以复用链接的 ,实际应该单例一下
public class HttpClientFactory
{
private static HttpClient _HttpClient = null;
static HttpClientFactory()
{
_HttpClient = new HttpClient(new HttpClientHandler());
}
public static HttpClient GetHttpClient()
{
return _HttpClient;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12