I'm trying to set the Content-Type
header of an HttpClient
object as required by an API I am calling.
(我正在尝试根据我正在调用的API设置HttpClient
对象的Content-Type
标头。)
I tried setting the Content-Type
like below:
(我尝试如下设置Content-Type
:)
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:
(它允许我添加Accept
标头,但是当我尝试添加Content-Type
它将引发以下异常:)
Misused header name.
(标头名称滥用。)
Make sure request headers are used with HttpRequestMessage
, response headers with HttpResponseMessage
, and content headers with HttpContent
objects. (确保HttpRequestMessage
使用请求标头, HttpResponseMessage
使用响应标头, HttpContent
对象使用内容标头。)
How can I set the Content-Type
header in a HttpClient
request?
(如何在HttpClient
请求中设置Content-Type
标头?)
ask by mynameiscoffey translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…