作者:慈悲的残忍 | 来源:互联网 | 2023-05-19 15:04
我在HttpClient上设置Content-Type时遇到问题.我按照这个问题:如何为HttpClient请求设置Content-Type标头?
但仍然没有运气.
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);
});
我也试过使用Flurl库,但在尝试添加'Content-Type'时它崩溃了.
misused header name content-type
那么我该如何强制它以便真正添加呢?提前致谢.
1> Ajay P..:
我想你应该试试这个
req.COntent= new StringContent(rcString, Encoding.UTF8, "application/json");
结帐此链接:
如何为HttpClient请求设置Content-Type标头?
编辑
删除此行c.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
并检查