【发布时间】:2016-12-18 07:46:26
【问题描述】:
在最近一次 Android-Webview 更新 (52.0.2743.98) 中,我很难使用 Cordova 将 cookie 发送到我的 websocket 服务器。
我正在使用 cookie 进行身份验证,并且此代码在更新之前运行良好:
this.websocket = new WebSocket(url);
我什至尝试了另一种在握手期间强制使用 cookie 标头的方法,但它不起作用:
this.websocket = new WebSocket(
url,
[],
{ headers: { Cookie: 'key=value' } }
);
这是服务器响应:
WebSocket connection to 'wss://api.my.url/dev/ws' failed:
Error during WebSocket handshake: Unexpected response code: 403
在 Chrome 的“网络”选项卡下:
Request:
Accept-Encoding:gzip, deflate
Accept-Language:en-GB,en-US;q=0.8
Cache-Control:no-cache
Connection:Upgrade
Host:api.host.com
Origin:file://
Pragma:no-cache
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:<random value>==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Linux; Android 6.0.1; Nexus 6P Build/MTC19X; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/52.0.2743.98 Mobile Safari/537.36
X-Requested-With:com.domain
Response:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:file://
Connection:keep-alive
Content-Encoding:gzip
Content-Type:application/json; charset=utf-8
Date:Thu, 11 Aug 2016 13:47:09 GMT
Server:nginx/1.8.1
Set-Cookie:key=<random id>; Path=/
Transfer-Encoding:chunked
Vary:Accept-Encoding
请注意,它仍然适用于 Chrome 和 IOS(以及以前版本的 Android-Webview)
有什么建议吗?
【问题讨论】:
-
不要认为我们可以通过协议在 websocket 上显式传递标头。但是,默认 cookie 应该已经通过每个请求正常 http 或 ws 。我在更新版本的 android 上也遇到了同样的问题。
-
您找到解决方案了吗?
标签: javascript android cordova cookies websocket