在最近的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)
有什么建议吗?
也许解决问题的更好选择是更换浏览器。现在你使用chrome浏览器来显示混合应用程序,你可以更改这个默认浏览器。
我对WebSockets有同样的问题,我使用了CrossWalk浏览器,我对WebSockets没有任何问题。您可以在此页面查看人行横道项目:https://crosswalk-project.org/documentation/android.html
我使用Ionic来实现我的应用程序,只有我必须运行这个命令来安装人行横道浏览器:
离子浏览器添加人行横道