汇联易接口.NET调用存在的坑

2023/12/21

这篇文章发布于 336 天前,部分信息可能已经发生变化。

问题

汇联易接口POSTMAN可以调通,C# HttpClient报错500.

{"timestamp":"2023-12-21T05:49:46.362+0000","path":"/gateway/oauth/token","status":500,"error":"Internal Server Error","message":"","requestId":"649c231a"}

分析问题

  • C# HttpClient测试好几种调用方法都不行,估计是POSTMAN的http post与C# HttpClient post有细节差异,启用Charles抓包分析

  • Charles抓包分析结果:C# HttpClient post头部多了:Expect:100-continue

image-20231221165322861

解决方法

  • C# HttpClient post头默认带:Expect:100-continue,DefaultRequestHeaders.ExpectContinue = false即可去除。
  using (var client = new HttpClient())
  {
      client.DefaultRequestHeaders.ExpectContinue = false;
  }