下面将提示继续。为验证服务器证书的连接添加一个自定义信任管理器 - 并添加一个自定义主机名验证逻辑,用于评估主机名是否来自可接受列表 - 或者只是硬编码为true。
SSLContext ctx = SSLContext.getInstance("TLS");
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(getTrustedCert());//todo
ctx.init(null, tmf.getTrustManagers(), null);
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return isInAcceptableHostNameList(arg0);//todo
}
});
从docs -
此类使用的HostnameVerifier和SSLSocketFactory的。有两个类定义了默认实现 。然而, 实现可以在每个类(静态)或每个实例基础上进行替换。在创建实例时,所有新的HttpsURLConnections实例将为 分配“默认”静态值,但在连接之前可以通过调用适当的每个实例设置方法 来覆盖它们。
那么这是https方式,但我打算使用普通的旧套接字作为双向通信是必需的,而不是简单的http请求和响应模型 – Allahjane