5

我有一个使用 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 来向请求添加一个信任库(并添加一个超时)

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);

一切正常。为什么超时不起作用?有人可以帮助我吗?提前致谢。

4

1 回答 1

7

原因是您正在设置httpClient之前的超时,请按此顺序进行

httpComponentsMessageSender.setReadTimeout(3*1000);       
httpComponentsMessageSender.setConnectionTimeout(3*1000);
httpComponentsMessageSender.setHttpClient(httpClient());
于 2019-03-03T18:33:22.770 回答