java - 如何设置 JAVA SSL 连接的持续时间
问题描述
我为我的安全 SSL 连接设置了 Java 系统属性,如下所示:
System.setProperty("https.protocols", "TLSv1.2")
System.setProperty("javax.net.ssl.keyStoreType", "PKCS12")
System.setProperty("javax.net.ssl.keyStore",keyStore)
System.setProperty("javax.net.ssl.keyStorePassword", keyStorePW)
System.setProperty("javax.net.ssl.trustStore",trustStore)
System.setProperty("javax.net.ssl.trustStorePassword", trustStorePW)
现在我做这样的事情:
- 发送 SOAP 请求 1
- 做其他事情
- 发送 SOAP 请求 2
如果“做其他事情”花费的时间超过 5 秒,我们将再次完成整个 SSL 握手(服务器问候、客户端问候等)。如果“做其他事情”的时间少于 5 秒,请求将立即发送
--> 如何将这个持续时间设置为超过 5 秒?
编辑:
这就是我进行 SOAP 调用的方式:
static String callSoap() {
SOAPMessage request = //..creating request
SOAPMessage response=dispatch.invoke(request)
SOAPBody responseBody=response.getSOAPBody()
.......
return....
}
解决方案
当您调用 时socket.connect()
,您可以在那里指定所需的超时时间。铁:
int timeout = 5000 * 3;
socket.setSoTimeout(timeout);
socket.connect(new InetSocketAddress(hostAddress, port), timeout);
SoTimeout
可能不需要;此超时是read()
在引发异常之前调用将阻塞的时间。如果您不希望任何超时读取,您可以将其设置为 0,并且您接受等待直到读取一个字节。
仅当完成该过程需要超过 15 秒时,才应尝试重新连接。
好的,在 SOAP 世界中,这样的事情应该可以解决问题:
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
URL endpoint =
new URL(new URL("http://yourserver.yourdomain.com/"),
"/path/to/webservice",
new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL url) throws IOException {
URL target = new URL(url.toString());
URLConnection connection = target.openConnection();
// Connection settings
connection.setConnectTimeout(10000); // 10 sec
connection.setReadTimeout(60000); // 1 min
return(connection);
}
});
SOAPMessage result = connection.call(soapMessage, endpoint);
在这里查看更多信息,可能会有所帮助。
推荐阅读
- python-3.x - 检查变量是否存在的Pythonic方法,如果存在,将其与另一个进行比较
- tornadofx - 透明视图
- javascript - 在子组件的输入中输入文本然后更新父组件状态在 React 中总是中断?
- javascript - 如何使用 webpack 动态导入
- angular - 更改拦截器 angular2 中的请求正文
- symfony - 解析错误:语法错误,意外的“?”,期望变量(T_VARIABLE)
- java - Java 可选:如何处理长 map() 链
- javascript - PrimeNg TabView 与 ConfirmDialog
- mysql - 使用递归 MySQL 查询从父行到子行获取值
- angular - 十进制管道,未四舍五入的值