在最新的 Android-Webview 更新 (52.0.2743.98) 中,我很难使用 Cordova 将 cookie 发送到我的 websocket 服务器。
我使用 cookie 进行身份验证,并且此代码在更新之前可以完美运行:
this.websocket = new WebSocket(url);
我什至尝试了另一种方法来在握手期间强制使用 cookie header ,但它不起作用:
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)
有什么推荐吗?
最佳答案
也许解决您的问题的更好选择是更换浏览器。现在您使用 Chrome 浏览器来显示混合应用程序,您可以更改此默认浏览器。
我在使用 WebSockets 时遇到了同样的问题,并且我使用了 CrossWalk 浏览器,并且在使用 WebSockets 时没有遇到任何问题。您可以在这个页面查看 CrossWalk 项目:https://crosswalk-project.org/documentation/android.html
我使用 Ionic 来实现我的应用程序,只需运行此命令即可安装 CrossWalk 浏览器:
ionic 浏览器添加人行横道
关于javascript - 在 Android 上使用 Cordova Websockets 发送 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38898584/