把一个自定义字符串"授权"在System.Net.HttpClient头 [英] Putting a custom string in "Authorization" header in System.Net.HttpClient
问题描述
我试图使用 System.Net.HttpClient
类来发布消息,谷歌云消息。
谷歌云信息,需要你有一个名为授权头,其格式与此类似:
键= AIzaSyBxFuZ9IbtGbJHX6F5wdTc1mHnB7i0Lu8D
但的HttpClient
类时,我尝试这会抛出异常。
KeyString中字符串=键= AIzaSyBxFuZ9IbtGbJHX6F5wdTc1mHnB7i0LJ0w;
使用(VAR的客户=新的HttpClient())
{
client.DefaultRequestHeaders.Add(富,KeyString中); //< ==证明我可以
client.DefaultRequestHeaders.Add(授权,KeyString中); //抛出异常
唯一的例外是类型 System.FormatException
。该消息是
价值键= AIzaSyBxFuZ9IbtGbJHX6F5wdTc1mHnB7i0LJ0w'的格式是 无效。
和调用堆栈是:
在System.Net.Http.Headers.HttpHeaderParser.ParseValue(字符串值,对象storeValue,的Int32和放大器;指数)
在System.Net.Http.Headers.HttpHeaders.ParseAndAddValue(字符串名称,HeaderStoreItemInfo信息,字符串值)
在System.Net.Http.Headers.HttpHeaders.Add(字符串名称,字符串值)
我怎样才能得到这头到这个职位的要求没有异常被抛出?
顺便说一句,我组成了这样一个帖子使用招,和它的作品:
标题:
的User-Agent:提琴手
授权:键= AIzaSyBxFuZ9IbtGbJHX6F5wdTc1mHnB7i0Lu8D
主持人:android.googleapis.com
内容长度:220
内容类型:应用程序/ JSON
请求体:
{
registration_ids: ["APA91bEM6XPdiZv5VgNNApakfyYfZwB871018Hljl1L27kaPvksasnR0bHlmcCZFxOSPD6bDLMZgvgfT9xsKnF6Tg0oSQM2cMM1KRbuK7cR7jICqAnSDYg_SvERTzPMT8puXGTlVkEVH6dsneBkXiBu6pZikWXWyRATAVbXnAHTe20-nQerb0"],
}
试试这个答案,而不是:
<一个href="http://stackoverflow.com/a/24575721/1981387">http://stackoverflow.com/a/24575721/1981387
好像有点变通办法的,因为它是一个字符串对,而不是一个很好的对象,但它似乎工作。
I am trying to use the System.Net.HttpClient
class to post a message to Google Cloud Messaging.
Google Cloud Message requires you to include a header called "Authorization", in a format similar to this:
key=AIzaSyBxFuZ9IbtGbJHX6F5wdTc1mHnB7i0Lu8D
But the HttpClient
class throws an exception when I try this.
string keyString = "key=AIzaSyBxFuZ9IbtGbJHX6F5wdTc1mHnB7i0LJ0w";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Foo", keyString); // <== Proving I can
client.DefaultRequestHeaders.Add("Authorization", keyString); // Exception thrown
The exception is of type System.FormatException
. The message is
The format of value 'key=AIzaSyBxFuZ9IbtGbJHX6F5wdTc1mHnB7i0LJ0w' is invalid.
And the callstack is:
at System.Net.Http.Headers.HttpHeaderParser.ParseValue(String value, Object storeValue, Int32& index)
at System.Net.Http.Headers.HttpHeaders.ParseAndAddValue(String name, HeaderStoreItemInfo info, String value)
at System.Net.Http.Headers.HttpHeaders.Add(String name, String value)
How can I get this header into this post request without an exception being thrown?
Incidentally, I have composed a post like this using Fiddler, and it works:
Headers:
User-Agent: Fiddler
Authorization: key=AIzaSyBxFuZ9IbtGbJHX6F5wdTc1mHnB7i0Lu8D
Host: android.googleapis.com
Content-Length: 220
Content-Type: application/json
Request Body:
{
"registration_ids" : ["APA91bEM6XPdiZv5VgNNApakfyYfZwB871018Hljl1L27kaPvksasnR0bHlmcCZFxOSPD6bDLMZgvgfT9xsKnF6Tg0oSQM2cMM1KRbuK7cR7jICqAnSDYg_SvERTzPMT8puXGTlVkEVH6dsneBkXiBu6pZikWXWyRATAVbXnAHTe20-nQerb0"],
}
Try this answer instead:
http://stackoverflow.com/a/24575721/1981387
Seems like a bit of a workaround, since it's a string pair instead of a nice object, but it seems to work.
这篇关于把一个自定义字符串&QUOT;授权&QUOT;在System.Net.HttpClient头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!