[英]C# HttpRequestMessage Content-Type force as "application/json" only
I'm creating an HttpClient request to post an object to an API endpoint.我正在创建一个 HttpClient 请求以将 object 发布到 API 端点。 The API endpoint only accepts "application/json" but my application changes the type to "application/json; charset=utf-8" which the API won't accept.
API 端点只接受“application/json”,但我的应用程序将类型更改为“application/json; charset=utf-8”,API 不接受。 What do I change in the StringContent to make it so it is only 'application/json'?
我要在 StringContent 中进行什么更改以使其仅是“应用程序/json”? I tried changing the encoding type from Default to null to UTF8 but that has not changed it.
我尝试将编码类型从 Default 更改为 null 到 UTF8 但这并没有改变它。
var httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) =>
{
return true;
};
var client = new HttpClient(httpClientHandler);
var webRequest = new HttpRequestMessage(HttpMethod.Post, base_url + url)
{
Content = new StringContent(body, Encoding.Default, "application/json")
};
Consider the following approach考虑以下方法
content.Headers.Remove("Content-Type"); content.Headers.Remove("内容类型"); // "{application/json; charset=utf-8}" content.Headers.Add("Content-Type", "application/json");
// "{application/json; charset=utf-8}" content.Headers.Add("Content-Type", "application/json");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.