[英]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.