我使用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)。所以我的服務器工作。
謝謝@ dave_thompson_085!有用! –