Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpRequestMessage中的奇怪行为';s内容类型标题_C#_.net_Http_Postman - Fatal编程技术网

C# HttpRequestMessage中的奇怪行为';s内容类型标题

C# HttpRequestMessage中的奇怪行为';s内容类型标题,c#,.net,http,postman,C#,.net,Http,Postman,我尝试从C#(.NETCore2.2.104)向外部web服务发出HTTP POST请求,请求主体中包含application/json 我已经阅读了SO中的所有类似问题,并编写了以下代码: SignXmlRequestDto requestBody = new SignXmlRequestDto(p12, model.SignCertPin, model.Data); string json = JsonConvert.SerializeObje

我尝试从C#(.NETCore2.2.104)向外部web服务发出HTTP POST请求,请求主体中包含application/json

我已经阅读了SO中的所有类似问题,并编写了以下代码:

            SignXmlRequestDto requestBody = new SignXmlRequestDto(p12, model.SignCertPin, model.Data);
            string json = JsonConvert.SerializeObject(requestBody);

            var httpRequestMessage = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = ncanNodeUrl,
                Headers =
            {
                { HttpRequestHeader.ContentType.ToString(), "application/json" }
            },
                Content = new StringContent(JsonConvert.SerializeObject(json))
            };

            var response = await httpClient.SendAsync(httpRequestMessage);

            string responseString = await response.Content.ReadAsStringAsync();
我从服务中得到一个错误,它说:“无效的标题内容类型。请将内容类型设置为application/json”。有趣的是,如果我模拟邮递员的请求,那么一切都很好,我得到了成功的响应

更新:根据@Kristóf Tóth的建议,我将代码修改为:

            var httpRequestMessage = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = ncanNodeUrl,
                Content = new StringContent(json, Encoding.UTF8, "application/json")
            };

            var response = await httpClient.SendAsync(httpRequestMessage);

            string responseString = await response.Content.ReadAsStringAsync();

但它仍然给我相同的错误信息。

这可能是编码问题。您应该使用JsonContent而不是StringContent,或者可以执行类似操作:

// Serialize into JSON String
var stringPayload = JsonConvert.SerializeObject(payload);

// Wrap JSON StringContent which then can be used by the HttpClient class
var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
内容类型是一个内容头。它应该在内容上设置,而不是在请求本身上设置。这可以使用构造函数来完成:

Content = new StringContent(JsonConvert.SerializeObject(json),Encoding.UTF8, "application/json")
或者通过设置StringContent的属性:

var content=new StringContent(JsonConvert.SerializeObject(json));
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

HttpRequestHeader.ContentType.ToString()
??@PanagiotisKanavos,在这个答案中,由于某种原因,它被建议不被接受或投票<代码>内容类型是初学者的内容标题。在
StringContent
构造函数中传递内容类型,或者在content objectUse Fiddler或其他调试代理上设置它,并捕获实际发送的内容。在StringContent构造函数中传递内容类型是每个人发布JSON请求的方式。邮递员的要求是什么样的?您的查询有何不同?这不是编码问题-错误反映的是内容类型标题,该标题未在内容中设置。你的答案确实如此,似乎它无法解决问题,仍然会得到相同的错误。你尝试了什么?这就是HTTP POST请求的方式。如果这不起作用,人们会注意到4年前当HttpClient出现时,第二个选项解决了我的问题——设置StringContent的Headers.ContentType属性,它就起作用了。现在我想知道,为什么第一个不起作用,而第二个起作用?我认为两者的动作是一样的…@Iskanderrambayev使用Fiddler并检查有什么不同。邮递员可以帮助您手工创建HTTP请求,但无法显示实际发送或接收的内容。小提琴手捕获both@IskanderRaimbayev您使用了哪个.NET版本?您是否通过NuGet包添加了HttpClient?在构造函数中传递内容类型非常常见,因此人们会注意到。虽然可能存在一些模糊的bug组合,例如在特定的HttpClient版本或.NET Core运行时中,但这仍然非常不可能。我使用.NET Core 2.2.104,首先尝试使用以下答案中的代码:。