NETCore HttpClient 以Post的方式调用API(不加密)

 1 public static string HttpClientPost(string url, object datajson)
 2 {
 3 HttpClient httpClient = new HttpClient();//http对象
 4 //表头参数
 5 httpClient.DefaultRequestHeaders.Accept.Clear(); 
 6 httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Value", "123"); //认证token
 7 httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
 8 
 9 
10 //转为链接需要的格式
11 HttpContent httpContent = new JsonContent(datajson);
12 //请求
13 HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
14 if (response.IsSuccessStatusCode)
15 {
16 Task<string> t = response.Content.ReadAsStringAsync();
17 if (t != null)
18 {
19 return t.Result;
20 }
21 }
22 return "";
23 }
View Code

 

posted @ 2019-12-12 13:41  沂水弦歌  阅读(877)  评论(0编辑  收藏  举报