1.进行HttpClient请求时,对接一些第三方厂商的接口时,需要设置

Content-Type:application/json;charset=utf-8

但是在进行http接口访问时,会自动在Content-Type结束位置与charset开始位置加空格,导致无法使用HttpClient请求接口数据。

Content-Type:application/json; charset=utf-8

需要使用http方式重构

content.Headers.ContentType = new MediaTypeHeaderValueClass("application/json");

需要把这个MediaTypeHeaderValueClass继承MediaTypeHeaderValue方法重写tostring里面参数方法即可。

// <summary>
///
/// </summary>
public class MediaTypeHeaderValueClass : MediaTypeHeaderValue
{
/// <summary>
///
/// </summary>
</param>
public MediaTypeHeaderValueClass(string mediaType) : base(mediaType)
{
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
if (string.IsNullOrWhiteSpace(base.CharSet))
{
}
}
}
 
调用方法
HttpClient client = new HttpClient(new HttpClientHandler
{
MaxConnectionsPerServer = 100000,
UseDefaultCredentials = false,
AllowAutoRedirect = false,
UseCookies = false,
Proxy = null,
UseProxy = false,
AutomaticDecompression = DecompressionMethods.GZip
});
var content = new StringContent(strParam);
var msg = new HttpRequestMessage();
msg.Content = content;
msg.Method = HttpMethod.Post;
msg.RequestUri = new Uri(Url);
var rest = await client.SendAsync(msg);

相关文章:

  • 2022-12-23
  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-11
  • 2021-12-03
猜你喜欢
  • 2022-12-23
  • 2021-11-28
  • 2021-06-21
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2022-02-28
相关资源
相似解决方案