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-Type
like below:
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("http://example.com/");
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json");
// ...
}
It allows me to add the Accept
header but when I try to add Content-Type
it throws the following exception:
Misused header name. Make sure request headers are used with
HttpRequestMessage
, response headers with HttpResponseMessage
, and
content headers with HttpContent
objects.
How can I set the Content-Type
header in a HttpClient
request?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…