.netcore 中的 HttpClient 自动在 header 中添加一个空格 [英] HttpClient in .netcore is automatically adding a space in header

查看:67
本文介绍了.netcore 中的 HttpClient 自动在 header 中添加一个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .netcore 中使用 HTTPClient,但在添加自定义接受标头时遇到问题.

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");

但在此之后,我看到的是自动添加了一个空格,导致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.

有什么方法可以删除 HttpClient 标头中的那个空格?常规方法不起作用,例如 trim,或者只是 replace 一旦它位于 DefaultRequestHeaders 中,我无法直接修改.

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.

推荐答案

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

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.

感谢您的帮助 =D

这篇关于.netcore 中的 HttpClient 自动在 header 中添加一个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆