简体   繁体   English

C# HttpRequestMessage Content-Type 强制为“application/json”

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

相关问题 c# HttpClient post response content-type application/json - c# HttpClient post response content-type application/json Owin / WebApi强制仅接受application / json Accept和Content-Type标头 - Owin/WebApi force to to accept only application/json Accept and Content-Type headers 当没有提供Content-Type''application / Json“时,C#[WebMethod]没有命中 - C# [WebMethod] is not hitting when Content-Type ''application/Json" is not provided 如何在C#4.0中读取内容类型为application / json的HTTP Post数据 - How to read HTTP Post data with content-type of application/json in C# 4.0 如何在 c# 中将 Google.Cloud.Tasks.V2 HttpRequest 的 Content-Type 设置为“application/json”? - How to set the Content-Type to "application/json" for Google.Cloud.Tasks.V2 HttpRequest in c#? 从 Azure App Configuration Store 获取 Value,其中 content-type 在 C# 中设置为 application/json - Get Value from Azure App Configuration Store where the content-type is set to application/json in C# 如何在C#中检查内容类型/应用程序文本 - how to check content-type/application text in c# 使用内容类型application / x-www-form-urlencoded解析从HTTP POST接收的C#中的JSON数据 - Parsing JSON data in C# received from HTTP POST with content-type application/x-www-form-urlencoded c#设置uploadFile内容类型 - c# setting uploadFile content-Type HttpRequestMessage 的 Content-Type 标头中的奇怪行为 - Strange behavior in HttpRequestMessage's Content-Type header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM