简体   繁体   English

无法使用c#HttpClient发送Content-Type标头

[英]Can't send Content-Type header with c# HttpClient

I'm trying to set a Content-Type header in ac# HttpClient request of "multipart/form-data; boundary=----WebKitFormBoundaryaYcxA3wAp5XMUV2w" . 我正在尝试在"multipart/form-data; boundary=----WebKitFormBoundaryaYcxA3wAp5XMUV2w" ac# HttpClient请求中设置Content-Type标头。

So far I've tried using TryAddWithoutValidation which does not throw any exception/error but when I watch the request in fiddler its just not added? 到目前为止,我已尝试使用TryAddWithoutValidation ,它不会抛出任何异常/错误,但当我在fiddler中查看请求时,它只是没有添加? See code below. 见下面的代码。

client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryaYcxA3wAp5XMUV2w");
response = client.PostAsync("https://example.com", byteContent).Result;

I've also tried converting the byte array I'm trying to send to a string and using StringContent but this throws an exception saying "multipart/form-data; boundary=----WebKitFormBoundaryaYcxA3wAp5XMUV2w" is invalid. 我也尝试将我想要发送的字节数组转换为字符串并使用StringContent但这会抛出一个异常,说"multipart/form-data; boundary=----WebKitFormBoundaryaYcxA3wAp5XMUV2w"无效。

StringContent testStringcontent = new StringContent(Encoding.Default.GetString(allContentBytes), Encoding.UTF8, "multipart/form-data; boundary=----WebKitFormBoundaryaYcxA3wAp5XMUV2w");
response = client.PostAsync("https://example.com", testStringcontent).Result;

I've tried all the similar questions suggestions and can't see to get any to send the header or not throw some sort of exception. 我已经尝试了所有类似的问题建议,并且看不到任何发送标题或不抛出某种异常。 Should I abandon this and use web client which I'm told is more flexible? 我应该放弃这个并使用我被告知更灵活的网络客户端吗?

Got this working with 得到了这个

ByteArrayContent byteContent = new ByteArrayContent(allContentBytes);
byteContent.Headers.Remove("Content-Type");
byteContent.Headers.Add("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryaYcxA3wAp5XMUV2w");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 带有自定义标头的 HttpClient POST 请求发送不正确的 Content-Type header 字段 - C# HttpClient POST Request with Custom Headers sends incorrect Content-Type header field 无法设置Content-Type标头 - Can't set Content-Type header c# HttpClient post response content-type application/json - c# HttpClient post response content-type application/json 如何在C#中使用具有复杂内容类型的HttpClient? - How to use HttpClient with complex content-type in c#? HttpClient 无法解析“UTF-8”内容类型 - HttpClient can't parse “UTF-8” Content-Type C# HttpClient,出现错误无法添加值,因为标头“内容类型”不支持多个值 - C# HttpClient, getting error Cannot add value because header 'content-type' does not support multiple values C#:在HTTP中使用Content-Type标头发送参数 - C#: Sending parameter with Content-Type Header in HTTP 使用 C# HttpClient 发送的 HEAD 请求返回的 Content-Type 不正确; 但是,邮递员返回正确的 Content-Type - Content-Type returned by HEAD request sent using C# HttpClient is incorrect; however, Postman returns the correct Content-Type 无法在 HttpResponseMessage 标头上设置 Content-Type 标头? - Can't set Content-Type header on HttpResponseMessage headers? 如何为 HttpClient 请求设置 Content-Type header? - How do you set the Content-Type header for an HttpClient request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM