添加HttpClient的头产生了一些值出现FormatException [英] Adding HttpClient headers generates a FormatException with some values

查看:1686
本文介绍了添加HttpClient的头产生了一些值出现FormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这发生编码对谷歌云通讯的范围内,但在其他地方适用



考虑以下几点:

  VAR HTTP =新的HttpClient(); 
http.DefaultRequestHeaders.Authorization =新AuthenticationHeaderValue(键= ​​XXX);

  VAR HTTP =新的HttpClient(); 
http.DefaultRequestHeaders.Add(授权,键= XXX);



这两者的产生出现FormatException:



<块引用>

System.FormatException:值密钥的格式= XXX无效




的解决方案是删除等号。




  1. 挖掘到反射镜显示有不减当年,添加迈上了一个新标头值时运行的验证和解析的代码。为什么这一切必要吗?不应该这样客户端仅仅是走出我们的方式?


  2. 你如何逃生等号,这样增加该值成功?



解决方案

不知道是否仍然相关,但最近我遇到了同样的问题,并能通过调用不同的方法来添加标题信息解决:

  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.

  1. 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?

  2. 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屋!

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