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 }