Java WebSocketClient设置cookie头

时间:2014-11-20 18:53:52

标签: java cookies header websocket wss

我正在使用此库 - Java-WebSocket

我正在使用安全的websockets并尝试在WebSocketClient中传递一个http cookie:

HashMap<String, String> header = new HashMap<String, String>();
header =this.sessionCookie;//session cookie is obtained from https authentication
new WebSocketClient( new URI(serverURL), new Draft_17(), header); //server url is wss://xyzsomething:1100

但这似乎不起作用(连接失败)。关于我做错了什么的任何想法?我没有在websocketclient中设置cookie吗?

1 个答案:

答案 0 :(得分:0)

因此我的套接字的问题是我必须与服务器交换证书。我必须在打开websocket之前执行以下函数:

private void trustServer() throws KeyManagementException, NoSuchAlgorithmException {  
    // Create a trust manager that does not validate certificate chains  
    TrustManager[] certs = new TrustManager[]{ new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
            return new java.security.cert.X509Certificate[]{};
        }
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException{ }
    }};
    SSLContext sslContext = null;
    sslContext = SSLContext.getInstance( "TLS" );
    sslContext.init( null, certs, new java.security.SecureRandom() );
    this.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext));
}