2016-12-29 43 views
0

我使用apache.httpcomponent.httpcore和httpclient版本4.3,我想使用httpclient發佈到我的https服務器。 但是當我使用wireshark捕獲數據包時,數據包是TCP而不是TLS。誰能告訴我爲什麼?使用TLS Apache httpclient,但無法在wireshark中捕獲tls數據包

以下代碼是我用trustmanager配置SSLContext。我在信任管理器中加載服務器的證書。

SSLContext ctx = null; 
String keystoreName = "/Users/user/ec_key/in_keystore"; 
char[] password = "123456".toCharArray();  //keystore's password 

    FileInputStream fIn; 
    KeyStore keystore; 
    TrustManagerFactory tmf=null; 

    try { 
     fIn = new FileInputStream(keystoreName); 
     keystore = KeyStore.getInstance("JKS"); 
     keystore.load(fIn, password);    //loading keysotre 

     tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); //TrustManagerFactory.getDefaultAlgorithm()=PKIX 
     tmf.init(keystore); 

     ctx = SSLContext.getInstance("TLSv1.2"); 
     // Initial SSLContext 
     ctx.init(null, tmf.getTrustManagers(), new java.security.SecureRandom()); 

     fIn.close(); 

    } catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (KeyStoreException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (NoSuchAlgorithmException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (CertificateException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (KeyManagementException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

     // create SSLConnectionSocketFactory 
     SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(ctx); 

    CloseableHttpClient httpClient = HttpClientBuilder.create() 
      .setSSLSocketFactory(factory) 
      .disableAutomaticRetries() 
      .build(); 

//execute http method 
HttpResponse httpResponse = httpClient.execute(method); 

我使用服務器的自簽名證書。我用

openssl s_client -connect 127.0.0.1:8443/webpage -CAfile test-ca.crt 

連接我的服務器。 test-ca.crt是我自己的CA的證書。結果是驗證返回碼是0(ok)。所以我的服務器工作。

回答

1

截獲數據包沒有問題。 Wireshark 基於(大部分)用作源和/或目的地的端口解碼用於顯示。它知道一些像443和465這樣的標準端口是SSL/TLS,但它不知道8443.

在消息列表窗格中右鍵單擊此會話的數據包並選擇DecodeAs ...,或者選擇一個數據包並單擊Analyze/DecodeAs ....並在版本2中單擊'+'(添加)按鈕;然後根據需要調整端口值(至8443)並在右側下拉菜單中(或在版本1列表框中)選擇SSL。

+0

謝謝@ dave_thompson_085!有用! –