Java WebSocketClient set cookie header

2k views Asked by At

I am using this library - Java-WebSocket

I am using secure websockets and trying to pass a http cookie in the WebSocketClient :

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

But this doesn't seem to be working (connection fails). Any ideas as to what am I doing wrong ? Am I not setting the cookie right in the websocketclient ?

1

There are 1 answers

0
vinzzz On

So the problem with my socket was that I had to exchange certificates with the server. I had to execute the following function before opening the 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));
}