java - https 在生产模式下运行需要很长时间
问题描述
所以我向其他服务器发送了一个 https 请求。我通过添加 set Timeout 属性来减少时间工作,需要 4-5 分钟才能得到服务器的响应。
用例:我在连接到服务器时没有发送用户名和密码,因为我需要捕获异常并向用户提供适当的消息。我正在使用 java.net 的 XmlRpcClientConfigImpl 客户端和 TrustManager 和 SSLContext 进行握手。
还做了一些日志记录,其中异常内的代码在 4-5 分钟后运行
使用xmlprc 从setTimeout 引用
我正在使用码头作为服务器这是一些代码,因为我不能分享很多
connected =true;
Proxy proxy = new Proxy(ServerVO.getHost(), ServerVO.getPort(), ServerVO.getUserName(), ServerVO.getPassword(), isHTTP);
int defaultConnTimeOut = SomeCass.getConnectionTimeOut();
int defaultReplyTimeOut = SomeCass.getReplyTimeOut();
SomeCass.setConnectionTimeOut(2000);
SomeCass.setReplyTimeOut(5000);
try {
logger.info("fetching versionnnnnnnn-----------------");
SomeCass.version();
}
catch (Exception e) {
logger.info("fetching versionnnnnnnn-------exceptionnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn----------");
connected = Boolean.FALSE;
}
finally {
SomeCass.setConnectionTimeOut(defaultConnTimeOut);
SomeCass.setReplyTimeOut(defaultReplyTimeOut);
}
超时代码
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
XmlRpcClient client = new XmlRpcClient();
config.setServerURL(new URL(serverURL));
config.setConnectionTimeout(xmlrpcConnTimeout);
config.setReplyTimeout(xmlrpcReplyTimeOut);
client.setConfig(config);
//在配置中设置主机用户名
代理类构造器上的握手代码
this.address = address;
this.port = port;
this.username = username;
this.password = password;
this.url = url;
if(!this.isHTTP) {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs,
String authType) {
// Trust always
}
public void checkServerTrusted(X509Certificate[] certs,
String authType) {
// Trust always
}
} };
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
// Create empty HostnameVerifier
HostnameVerifier hv = new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
};
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
解决方案
找到了解决方案
将此属性添加到 XMLRPCclient
client.setTransportFactory(new XmlRpcCommonsTransportFactory(client));
它为每个请求创建一个新实例
https 仍然比 http 多花 5 秒,但我想这是预期的行为,因为 https 通常比 http 慢。如果有人能建议如何进一步减少这个时间,那将是一个很大的帮助