简体   繁体   English

Java WebSocketClient设置cookie标头

[英]Java WebSocketClient set cookie header

I am using this library - Java-WebSocket 我正在使用此库-Java-WebSocket

I am using secure websockets and trying to pass a http cookie in the WebSocketClient : 我正在使用安全的websocket,并尝试在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

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 ? 我不是在websocketclient中设置cookie吗?

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 : 打开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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM