我使用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!有用! –