首页 > 解决方案 > 如何设置 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) 

现在我做这样的事情:

如果“做其他事情”花费的时间超过 5 秒,我们将再次完成整个 SSL 握手(服务器问候、客户端问候等)。如果“做其他事情”的时间少于 5 秒,请求将立即发送

--> 如何将这个持续时间设置为超过 5 秒?

编辑:

这就是我进行 SOAP 调用的方式:

static String callSoap() {

       SOAPMessage request = //..creating request
        
       SOAPMessage response=dispatch.invoke(request)

       SOAPBody responseBody=response.getSOAPBody()

   .......
   
   return....
  }

标签: javasslsoap

解决方案


当您调用 时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);

这里查看更多信息,可能会有所帮助。


推荐阅读