简体   繁体   English

httpclient - 如何在多部分中将Content-Disposition设置为“application / json;”

[英]httpclient - how to set Content-Disposition to “application/json;” in a multipart

Using System.Net.Http.Httpclient, I am trying to do a multipart post in C# and with a wp8. 使用System.Net.Http.Httpclient,我试图在C#中使用wp8进行多部分发布。

This is a snippet of my code: 这是我的代码片段:

varclient = new HttpClient();

client.DefaultRequestHeaders.TryAddWithoutValidation(    
"Content-Type", "application/json");

content = new MultipartFormDataContent();
content.Add(new StringContent(requestObj, Encoding.UTF8, "application/json"), "request");

but using Fiddler, I noticed that I am sending this: 但是使用Fiddler,我注意到我发送了这个:

Content-Disposition: form-data; 内容处理:表格数据; name=request 名称=请求

Content-Type: text/plain; 内容类型:text / plain; charset=utf-8 字符集= utf-8的

while I need to send this (taken from an android device where the call is working): 虽然我需要发送这个(取自调用工作的Android设备):

Content-Disposition: application/json; Content-Disposition:application / json; name="request" 名称=“请求”

Content-Type: text/plain; 内容类型:text / plain; charset=UTF-8 字符集= UTF-8

  • How to achieve the expected result? 如何达到预期效果?

What about: 关于什么:

setting the header on the HttpContent using TryAddWithoutValidation 使用TryAddWithoutValidationHttpContent上设置标头

and changing the MultipartFormDataContent into a MultipartContent object: 并将MultipartFormDataContent更改为MultipartContent对象:

var content = new MultipartContent();

var contentData = new StringContent(requestObj, Encoding.UTF8, "application/json");
contentData.Headers.TryAddWithoutValidation("Content-Disposition", "application/json name=request");
content.Add(contentData);

This results in these headers 这导致这些标题

POST http://www.directupload.net/index.php?mode=upload HTTP/1.1
Content-Type: multipart/mixed; boundary="6905763f-e85a-44f9-b7f4-8967b357addf"
Host: www.directupload.net
Content-Length: 274
Expect: 100-continue
Connection: Keep-Alive

--6905763f-e85a-44f9-b7f4-8967b357addf
Content-Type: application/json; charset=utf-8
Content-Disposition: application/json name=request

{    "id": 1,    "name": "A green door",    "price": 12.50,    "tags": ["home", "green"]}
--6905763f-e85a-44f9-b7f4-8967b357addf--

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM