编程语言
首页 > 编程语言> > c#-httpclient-如何在多部分中将Content-Disposition设置为“ application / json;”

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

作者:互联网

我正在尝试使用System.Net.Http.Httpclient在C#和wp8中编写多篇文章.

这是我的代码的片段:

varclient = new HttpClient();

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

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

但是使用Fiddler,我注意到我发送的是:

内容处置:表单数据;名称=请求

内容类型:文本/纯文本;字符集= utf-8

而我需要发送此消息(取自通话正常的android设备):

内容处置:application / json; name =“ request”

内容类型:文本/纯文本;字符集= UTF-8

>如何达到预期效果?

解决方法:

关于什么:

使用TryAddWithoutValidation在HttpContent上设置标头

并将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);

这导致这些标题

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--

标签:windows-phone-8,dotnet-httpclient,multipart,c
来源: https://codeday.me/bug/20191028/1953285.html