HttpClient
개체의 Content-Type
헤더를 내가 요청한 API에 맞게 설정하려고합니다. HttpClient 요청에 대해 Content-Type 헤더를 어떻게 설정합니까?
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");
// ...
}
그것은 나를 Accept
헤더를 추가 할 수 있습니다하지만 난 Content-Type
을 추가하려고 할 때 다음과 같은 예외가 발생합니다 :
시되는 헤더 이름을. 요청 헤더가
HttpRequestMessage
, 응답 헤더가HttpResponseMessage
및 콘텐츠 헤더가HttpContent
인 객체와 함께 사용되는지 확인하십시오.
HttpClient
요청에 Content-Type
헤더를 어떻게 설정할 수 있습니까?
다른 방법으로는'Response.Content.Headers'가 대부분 작동합니다. –
@AshishJain ASP.Net 웹 API에 대한'Response.Content.Headers'와 관련된 대부분의 응답은 작동하지 않지만'HttpContext.Current.Response.ContentType'을 사용하여 쉽게 설정할 수 있습니다 if 너 필요해. – jerhewet
@ jerhewet 나는 나를 위해 일한 방식대로 사용했다. var content = new StringContent (data, Encoding.UTF8, "application/json"); –