CXF Soap 客户端首次连接失败 运行

CXF Soap Client fails to connect on first run

当我创建一个 cxf 客户端来调用 SOAP Web 服务时,它 returns connection refusedUnexpected end of file from server。问题是,它只是在第一次请求时发生,之后它就像客户端 "warmed up" 并且实际开始工作。

知道为什么会这样吗?

...
if(port == null) {
  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  factory.getInInterceptors().add(new LoggingInInterceptor());
  factory.getOutInterceptors().add(new LoggingOutInterceptor());
  factory.setAddress("http://localhost:9000/helloWorld");
  port = factory.create(HelloWorld.class);

  ... 
  Client client= ClientProxy.getClient(port);
  HTTPConduit http = (HTTPConduit) client.getConduit();
  HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
  httpClientPolicy.setAllowChunking(false);
  http.setClient(httpClientPolicy);
}

port.someMethod();  // fails on first run, but succeeds on all following runs
...

删除 if 语句会导致客户端在每次调用时都失败,而不仅仅是第一次。我真的被困住了,非常感谢任何帮助。

这个问题似乎是由 TLS 1.2 引起的。在 Java8 中,default TLS version changed 从 TLSv1 到 TLSv1.2。即使使用最新版本的 CXF (3.1.7),TLS 1.2 似乎也不起作用。所以解决方案是设置用于 CXF 的 TLS 版本:

...
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setAllowChunking(false);
httpClientPolicy.setAutoRedirect(true);
httpClientPolicy.setConnection(ConnectionType.KEEP_ALIVE);

String proxyUrl = "http://proxy.com";
String proxyPortString = "8080";
HTTPConduit http = (HTTPConduit)client.getConduit();
SSLContext sslContext = SSLContext.getInstance("TLSv1");
sslContext.init(null, null, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
TLSClientParameters tlsClientParameters = new TLSClientParameters();
tlsClientParameters.setUseHttpsURLConnectionDefaultSslSocketFactory(true);

http.setTlsClientParameters(tlsClientParameters);
http.setClient(httpClientPolicy);