在HttpClient中显式设置要获取操作的内容类型标题 [英] Explicitly Set Content-Type Headers For Get Operation in HttpClient
问题描述
有没有一种方法可以在执行 GET
时显式设置 Content-Type
标头值 HttpClient
?
我意识到这打破了1.1协议,但是我使用的是不符合该协议的API ,并要求我设置 Content-Type
标头。
我尝试了此操作,但无济于事...
使用(var httpClient = new HttpClient())
{
var httpRequestMessage = new HttpRequestMessage(HttpMethod .get, http://example.com);
httpClient.DefaultRequestHeaders.TryAddWithoutValidation( Content-Type, application / x-www-form-urlencoded + v1.3);
等待httpClient.SendAsync(httpRequestMessage)
}
I在添加 TryAddWithoutValidation
之后检查了 DefaultRequestHeaders
,它似乎没有设置 Content-Type
值。
如果我尝试设置httpRequestMessage的Content-Type(通过设置 httpRequestMessage.Content = ...
,出现以下错误:
无法发送具有这种动词类型的内容主体。
有没有一种方法可以显式设置 Content-Type
使用HttpClient进行 GET
操作?
基于我的发现,我得出结论: HttpClient在协议规则方面非常严格。我还通过实现DLL进行了反思,但找不到任何表明它允许违反协议的内容。
我认为当您尝试设置内容类型标头时,异常消息会被HttpClient强制执行。是自描述性的:
System.InvalidOperationException:滥用的标头n阿姆。确保请求标头与HttpRequestMessage一起使用,响应标头与HttpResponseMessage一起使用,以及内容标头与HttpContent对象一起使用。
如果使用set内容主体,您还会收到另一条自我描述的消息:
System.Net.ProtocolViolationException:无法与此发送内容主体动词类型。
由于您愿意违反GET请求的HTTP规则,因此我确定您唯一的选择是坚持使用限制性较小的WebClient,在这种情况下也可以使用。
Is there a way in which I can explicitly set the Content-Type
header values when performing a GET
with HttpClient
?
I realise this breaks 1.1 protocol, but I am working with a API that does not conform to it, and REQUIRES I set a Content-Type
Header.
I have tried this with to no avail...
using (var httpClient = new HttpClient())
{
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://example.com");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded+v1.3");
await httpClient.SendAsync(httpRequestMessage)
}
I've inspected the DefaultRequestHeaders
after the TryAddWithoutValidation
is added, and it does not seem to be setting the Content-Type
value.
If I try to set the Content-Type of the httpRequestMessage (by setting httpRequestMessage.Content = ...
, I get the following error:
Cannot send a content-body with this verb-type.
Is there a way that I can explicitly set the Content-Type
for a GET
operation using the HttpClient?
Based on my findings i concluded the HttpClient is very restrictive in terms of the protocol rules. I also reflected through the implementation DLL and i couldn't find anything that it would indicate that it allows protocol violations.
GET requests shouldn't have content-type headers, and the HttpClient is enforcing that rule.
I think the exception message when you try to set the content-type header is self-descriptive:
System.InvalidOperationException: Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.
Also if you use set the content body you get one more self-descriptive message:
System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
Since you are willing to violate HTTP rules for GET requests i am pretty sure your only option is to stick with the less restrictive WebClient, which works in that scenario.
这篇关于在HttpClient中显式设置要获取操作的内容类型标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!