How do you set the Content-Type header for an HttpClient request?

后端 未结 14 2226
暗喜
暗喜 2020-11-22 03:57

I\'m trying to set the Content-Type header of an HttpClient object as required by an API I am calling.

I tried setting the Content-Ty

14条回答
  •  孤独总比滥情好
    2020-11-22 04:28

    Ok, it's not HTTPClient but if u can use it, WebClient is quite easy:

    using (var client = new System.Net.WebClient())
     {
        client.Headers.Add("Accept", "application/json");
        client.Headers.Add("Content-Type", "application/json; charset=utf-8");
        client.DownloadString(...);
     }
    

提交回复
热议问题