2016-12-29 105 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!有用! –

相关问题