.net Core System.Net.Http.HttpClient 发送post请求

 

string url = "http://www.xxx.com/api/postmsg";

string jsonContent = JsonConvert.SerializeObject(message);

using (var client = new HttpClient())
{
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
//上面代码使http Content-Type 为 application/json; charset=utf-8。如果希望Content-Type为application/json,可以使用下面两行代码
//content.Headers.Remove("Content-Type"); // "{application/json; charset=utf-8}"
//content.Headers.Add("Content-Type", "application/json");

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer");
string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
}

.net Core System.Net.Http.HttpClient 发送post请求

上一篇:web 中怎么实现斜线表头效果?


下一篇:超全的HTTP请求响应码详细解析