2014-11-20 75 views
2

我使用這個庫 - Java-WebSocket的Java WebSocketClient設置cookie頭

我使用安全的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嗎?

+0

'this.sessionCookie'是一個HashMap?你從哪裏得到它的?你填寫的關鍵是什麼? – MOnsDaR 2017-05-03 11:42:19

回答

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)); 
}