问题描述
我有与相同的问题,我有
使用,以及查看许多其他API,例如Jetty和。但是尽管这样我无法打开websocket连接。
我有一个,需要使用WebSocket对服务器进行身份验证。什么是正确的方式将cookie转换为httpHeader,或者有任何简单的方法来简单地添加整个cookie在认证时连接到websocket?也许我只是缺少明显的..
对可能的误用条款道歉,但我希望你能得到一个大致的想法。
任何帮助将非常感谢,谢谢!
所以我实际上设法解决它,证明它不是cookie是实际的问题,而是websocket没有用有效的sslcontext初始化。这很容易解决:
WebSocketOrderClient webSocketOrderClient = new WebSocketOrderClient(uri,new Draft_17(),cmap,TIMEOUT);
SSLContext sslContext = null;
sslContext = SSLContext.getInstance(TLS);
sslContext.init(null,null,null); //将使用java的默认密钥和信任存储,这是足够的,除非你处理自签名证书
webSocketOrderClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext));
webSocketOrderClient.connectBlocking(); WebSocketOrderClient:
private class WebSocketOrderClient extends WebSocketClient {
public WebSocketOrderClient(URI serverUri,Draft draft,Map< String,String> headers,int timeout){
super(serverUri,draft,headers,timeout );
}
@Override
public void onOpen(ServerHandshake handshakedata){
Log.w(connected,true);
}
@Override
public void onMessage(String message){
Log.w(got:,message);
}
@Override
public void onClose(int code,String reason,boolean remote){
Log.w(Disconnected,+ code)
}
@Override
public void onError(Exception ex){
ex.printStackTrace();
}
}
希望这可以帮助任何可能遇到这个问题的人未来。
I have the same issue as Websockets and cookies in Android, and I have been trying to solve it as the first comment suggested,
using Java-WebSocket, as well as looking at many other APIs such as Jetty and AndroidAsync. But despite this I am unable to open up a websocket connection.
I have an Apache http cookie, and need this to authenticate myself to the server with a WebSocket. What is the correct way of translating a cookie into a httpHeader, or is there any neat way to simply add the entire cookie in the authentication when connection to a websocket? Maybe I am just missing the obvious..
Apologies for possible misuses of terms, but I hope you get the general idea.
Any help would be much appreciated, thank you!
So I actually managed to solve it, and it turned out that it was not the cookie that was the actual issue, but rather that the websocket is not initialized with a valid sslcontext. This was solved rather easily by:
WebSocketOrderClient webSocketOrderClient = new WebSocketOrderClient(uri, new Draft_17(), cmap, TIMEOUT);
SSLContext sslContext = null;
sslContext = SSLContext.getInstance( "TLS" );
sslContext.init( null, null, null ); // will use java's default key and trust store which is sufficient unless you deal with self-signed certificates
webSocketOrderClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext));
webSocketOrderClient.connectBlocking();
with WebSocketOrderClient:
private class WebSocketOrderClient extends WebSocketClient {
public WebSocketOrderClient( URI serverUri, Draft draft, Map<String, String> headers, int timeout) {
super( serverUri, draft, headers, timeout );
}
@Override
public void onOpen( ServerHandshake handshakedata ) {
Log.w("connected", "true");
}
@Override
public void onMessage( String message ) {
Log.w( "got: ", message );
}
@Override
public void onClose( int code, String reason, boolean remote ) {
Log.w( "Disconnected", ""+code );
}
@Override
public void onError( Exception ex ) {
ex.printStackTrace();
}
}
Hope this helps anyone who might run into this problem in the future.
这篇关于使用身份验证cookie打开WebSocket连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!