添加HttpClient的头产生了一些值出现FormatException [英] Adding HttpClient headers generates a FormatException with some values
问题描述
这发生编码对谷歌云通讯的范围内,但在其他地方适用
考虑以下几点:
VAR HTTP =新的HttpClient();
http.DefaultRequestHeaders.Authorization =新AuthenticationHeaderValue(键= XXX);
和
VAR HTTP =新的HttpClient();
http.DefaultRequestHeaders.Add(授权,键= XXX);
这两者的产生出现FormatException:
<块引用>
System.FormatException:值密钥的格式= XXX无效
的解决方案是删除等号。
-
挖掘到反射镜显示有不减当年,添加迈上了一个新标头值时运行的验证和解析的代码。为什么这一切必要吗?不应该这样客户端仅仅是走出我们的方式?
-
你如何逃生等号,这样增加该值成功?
不知道是否仍然相关,但最近我遇到了同样的问题,并能通过调用不同的方法来添加标题信息解决:
VAR HTTP =新的HttpClient();
http.DefaultRequestHeaders.TryAddWithoutValidation(授权,键= XXX);
This occurred within the context of coding against Google Cloud Messaging, but applies elsewhere.
Consider the following:
var http = new HttpClient();
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("key=XXX");
and
var http = new HttpClient();
http.DefaultRequestHeaders.Add("Authorization", "key=XXX");
both of which generate a FormatException:
System.FormatException : The format of value key=XXX' is invalid.
The solution is to remove the equals sign.
Digging into reflector shows there is oodles of validation and parsing code that runs when adding a a new header value. Why is all this necessary? Shouldn't this client just be getting out of our way?
How do you escape the equals sign so that adding this value succeeds?
Not sure if still relevant, but I recently ran into this same issue and was able to solve it by calling a different method to add the header information:
var http = new HttpClient();
http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "key=XXX");
这篇关于添加HttpClient的头产生了一些值出现FormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!