HttpClient in .netcore is automatically adding a space in header

被刻印的时光 ゝ 提交于 2021-01-27 07:55:16

问题


I'm using HTTPClient in .netcore but I'm facing an issue when I add a custom accept 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");

But after this, what I see is that a space has been automaticaly added, resulting in "application/vnd.pagseguro.com.br.v3+json; charset=ISO-8859-1". That space is causing me a trouble because the server return me an error. Without that space it works.

There is any way to remove that space in HttpClient header? Conventional ways do not work, like trim, or just a replace once it's inside a DefaultRequestHeaders and I can't modify directly.


回答1:


I was able to turnarround this issue by making a new microservice with .net Framework 4.5. Using HttpWebRequest, the header dont add an automaticaly space between the charset and accept.

Thanks for the help =D




回答2:


This is a bug in corefx.

Still open after 3 years!

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



来源:https://stackoverflow.com/questions/40152607/httpclient-in-netcore-is-automatically-adding-a-space-in-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!