HttpClient的Content-Type设置

HttpClient的Content-Type设置

最近在对接公司内容的一个云服务的时候,遇到一个问题,就是如果使用HttpClient如何设置post时候的Content-Type?

 public static string PostAdminSelect(string start)
{
    string url = $"{BaseUrl}admin/select";
    Model.AdminSelectQuery adminSelectQuery = new Model.AdminSelectQuery();
    adminSelectQuery.start = start;
    string res = adminSelectQuery.ToJson();
    StringContent stringContent = new StringContent(res);
    stringContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
    string result = httpClient.PostAsync(url, stringContent).Result.Content.ReadAsStringAsync().Result;
    return result;
}

这样就可以随意的设置Content-Type的值了。

猜你喜欢

转载自www.cnblogs.com/zzr-stdio/p/10165539.html
今日推荐