.netcore 中的 HttpClient 会自动在 header 中添加一个 space

HttpClient in .netcore is automatically adding a space in header

我在 .netcore 中使用 HTTPClient 但我在添加自定义接受 header.

时遇到问题
 var client = new HttpClient { Timeout = new TimeSpan(0, 5, 0)};
 client.DefaultRequestHeaders.Clear();
 client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1");

但在此之后,我看到的是自动添加了 space,结果是“application/vnd.pagseguro.com.br.v3+json; charset=ISO-8859-1”。 space 给我带来了麻烦,因为服务器 return 我出错了。没有那个 space 它就可以工作。

有什么方法可以删除 HttpClient header 中的 space?传统的方法不起作用,比如 trim,或者只是一个 replace 一旦它在 DefaultRequestHeaders 里面,我不能直接修改。

我能够通过使用 .net Framework 4.5 创建一个新的微服务来解决这个问题。使用 HttpWebRequest,header 不要在字符集和接受之间自动添加 space。

感谢帮助=D

这是 corefx 中的错误。

3 年后仍然开放!

https://github.com/dotnet/corefx/issues/39260