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的值了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM