C2DM 授权密钥 CRLF 和 .Net WebRequest
我们正在使用 C2DM 并用 C# 编写服务器应用程序。
我们使用 WebRequest 组件通过 POST 获取授权密钥。这行得通,我们会得到一个编码的授权密钥。当我们将此密钥添加到要发送消息的请求的 auth 标头时,问题就出现了。
WebRequest request = WebRequest.Create("url");
...
request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + AuthorizationKey);
由于密钥具有 LF 字符,我们会得到一个异常...
Unhandled Exception: System.ArgumentException: Specified value has invalid CRLF characters.
Parameter name: value
at System.Net.WebHeaderCollection.CheckBadChars(String name, Boolean isHeaderValue)
at System.Net.WebHeaderCollection.Add(String name, String value)
CheckBadChars() 似乎拒绝授权密钥,因为它认为密钥中有一个 CRLF,而实际上只有一个 LF。
有谁知道我们如何解决这个问题?
We are using C2DM and are writing the server app in C#.
We obtain the authorization key with a POST using the WebRequest component. This works and we get back an encoded authorization key. The problem comes when we add this key to the auth header of the request that is going to send the message.
WebRequest request = WebRequest.Create("url");
...
request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + AuthorizationKey);
Since the key has the LF character, we get an exception...
Unhandled Exception: System.ArgumentException: Specified value has invalid CRLF characters.
Parameter name: value
at System.Net.WebHeaderCollection.CheckBadChars(String name, Boolean isHeaderValue)
at System.Net.WebHeaderCollection.Add(String name, String value)
It would seem that CheckBadChars() is refusing the authorization key because it thinks there is a CRLF in the key, when in fact there is only a LF.
Does anyone have any ideas how we could get around this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许你只是在最后得到了一个应该删除的换行符?
编辑:我阅读了文档(位于 http: //code.google.com/apis/accounts/docs/AuthForInstalledApps.html#Response),它说您应该得到如下所示的响应:
有三个换行符。因此,您需要做的是选择以 Auth= 开头的行并从那里获取授权令牌。
Maybe you’re just getting a linebreak at the end that you’re supposed to remove?
Edit: I read the documentation (at http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#Response) and it says you’re supposed to get back a response that looks like this:
There’s your three linebreaks. So what you need to do is pick out the the line starting with
Auth=
and grab the authorization token from there.