简体   繁体   English

无法设置Content-Type标头

[英]Can't set Content-Type header

I'm having trouble setting the Content-Type on HttpClient. 我在HttpClient上设置Content-Type时遇到问题。 I followed along this question: How do you set the Content-Type header for an HttpClient request? 我按照这个问题: 如何为HttpClient请求设置Content-Type标头? But still no luck. 但仍然没有运气。

String rcString = JsonConvert.SerializeObject(new RoadsmartChecks() { userguid = user_guid, coords = coordinates, radius = (radius * 100) + "" }, ROADSMART_JSON_FORMAT, JSONNET_SETTINGS);
HttpClient c = new HttpClient();
c.BaseAddress = new Uri(BASE_URL);
c.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); //Keeps returning false
c.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", hash_aes);
c.DefaultRequestHeaders.TryAddWithoutValidation("Roadsmart-app", Constant.APP_ID);
c.DefaultRequestHeaders.TryAddWithoutValidation("Roadsmart-user", user_guid);
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, BASE_URL + URL_CHECKS + "/fetch");
req.Content = new StringContent(rcString);
await c.SendAsync(req).ContinueWith(respTask =>
{
    Debug.WriteLine("Response: {0}", respTask.Result);
});

调试器 I also tried by using the Flurl library, but it crashes when trying to add the 'Content-Type'. 我也试过使用Flurl库,但在尝试添加'Content-Type'时它崩溃了。

misused header name content-type

So how can I force it so it really adds it? 那么我该如何强制它以便真正添加呢? Thanks in advance. 提前致谢。

I think you should try this 我想你应该试试这个

req.Content = new StringContent(rcString, Encoding.UTF8, "application/json");

checkout this links : 结帐此链接:

How do you set the Content-Type header for an HttpClient request? 如何为HttpClient请求设置Content-Type标头?

Edit 编辑

Remove this line c.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); 删除此行c.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); and check 并检查

UPDATE: See new answer for non-default content types 更新:查看非默认内容类型的新答案

With Flurl you shouldn't need to set Content-Type to application/json for methods like PostJsonAsync . 使用Flurl,您不需要为PostJsonAsync方法将Content-Type设置为application / json。 This is the default content type in this case and it will get set for you. 在这种情况下,这是默认的内容类型,它将为您设置。

The latest and greatest answer to this with Flurl is to upgrade . Flurl对此的最新和最好的答案是升级 2.0 introduces several enhancements in the headers dept: 2.0在header dept中引入了几个增强功能:

  1. They're no longer validated. 他们不再被证实。 Flurl now uses TryAddWithoutValidation under the hood, so you'll never get the "misused header name" error with the WithHeader(s) methods. Flurl现在使用了TryAddWithoutValidation ,因此您永远不会使用WithHeader(s)方法获得“误用标题名称”错误。 (I always found that validation behavior to be a bit overprotective.) (我总是发现验证行为有点过度保护。)

  2. In a fluent call they're set at the individual request level rather than the FlurlClient level, so you won't run into concurrency issues when reusing the client. 在一个流畅的调用中,它们被设置为单独的请求级别而不是FlurlClient级别,因此在重用客户端时不会遇到并发问题

  3. Since hyphens are common in header names but not allowed in C# identifiers, there's a new convention where underscores are converted to hyphens so you don't have to give up object notation when specifying multiple: 由于连字符在标题名称中很常见但在C#标识符中不允许,因此有一个新的约定 ,其中下划线转换为连字符,因此在指定多个时不必放弃对象表示法:

     url.WithHeaders(new { Content_Type = "foo", ... } 

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

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