我有一个使用SOAP向服务器发送消息的代码。我的代码看起来像这样:
private WebServiceTemplate makeTemplate() throws Exception {
WebServiceTemplate localTemplate = new WebServiceTemplate();
Jaxb2Marshaller marshaller = makeMarshaller();
localTemplate.setMarshaller(marshaller);
localTemplate.setUnmarshaller(marshaller);
localTemplate.setMessageSender(httpComponentsMessageSender()); //<-- Add HttpComponentsMessageSender
return localTemplate;
}
我创建了一个HttpComponentsMessageSender来为请求添加一个Trust Store(并添加一个超时)
public HttpComponentsMessageSender httpComponentsMessageSender() throws Exception {
HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
httpComponentsMessageSender.setHttpClient(httpClient());
httpComponentsMessageSender.setConnectionTimeout(30 * 1000);
httpComponentsMessageSender.setReadTimeout(30 * 1000);
return httpComponentsMessageSender;
}
public HttpClient httpClient() throws Exception {
return HttpClientBuilder.create().setSSLSocketFactory(sslConnectionSocketFactory())
.addInterceptorFirst(new RemoveSoapHeadersInterceptor()).build();
}
public SSLConnectionSocketFactory sslConnectionSocketFactory() throws Exception {
// NoopHostnameVerifier essentially turns hostname verification off as otherwise following error
// is thrown: java.security.cert.CertificateException: No name matching localhost found
return new SSLConnectionSocketFactory(sslContext() /*, NoopHostnameVerifier.INSTANCE */);
}
public SSLContext sslContext() throws Exception {
return SSLContextBuilder.create()
.loadTrustMaterial(MyTrustStore, MyPasswd.toCharArray()).build();
}
但是当我使用我的模板向服务器发送消息时,我收到此错误:
SEVERE: null
java.lang.UnsupportedOperationException
at org.apache.http.impl.client.InternalHttpClient.getParams(InternalHttpClient.java:211)
at org.springframework.ws.transport.http.HttpComponentsMessageSender.setConnectionTimeout(HttpComponentsMessageSender.java:137)
at
cl.newit.wstgr.main.WStgr.httpComponentsMessageSender(WStgr.java:308)
at cl.newit.wstgr.main.WStgr.makeTemplate(WStgr.java:192)
at cl.newit.wstgr.main.WStgr.genericSend(WStgr.java:165)
at cl.newit.wstgr.main.WStgr.sendM1(WStgr.java:84)
at cl.newit.wstgr.main.tester.TestM1(tester.java:236)
at cl.newit.wstgr.main.tester.main(tester.java:67)
如果我删除了超时行:
// httpComponentsMessageSender.setConnectionTimeout(30 * 1000);
// httpComponentsMessageSender.setReadTimeout(30 * 1000);
一切正常。 为什么超时不起作用? 有人可以帮帮我吗? 提前谢谢。
答案 0 :(得分:0)
原因是您要在超时前设置httpClient
,并按此顺序进行设置
httpComponentsMessageSender.setReadTimeout(3*1000);
httpComponentsMessageSender.setConnectionTimeout(3*1000);
httpComponentsMessageSender.setHttpClient(httpClient());