gpt4 book ai didi

java - Spring WebService - 无法调用需要使用用户名和密码代理的服务

转载 作者:行者123 更新时间:2023-12-01 11:09:40 27 4
gpt4 key购买 nike

我正在尝试在 Java 代码中使用 Spring WebServiceTemplate 来调用供应商提供的 protected Web 服务

这是我的 webserviceTemplate 的 spring 配置

    <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="messageFactory"/>
<property name="messageSender">
<bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
<property name="httpClient" ref="httpClient"/>
<property name="credentials" ref="credentials" />
</bean>
</property>
</bean>

<bean id="credentials" class="org.apache.http.auth.UsernamePasswordCredentials">
<constructor-arg value="${wsdl.username}:${wsdl.password}"/>
<!-- <constructor-arg index="0" value="${wsdl.username}"/>
<constructor-arg index="1" value="${wsdl.password}"/> -->
</bean>

<bean id="proxy" class="org.apache.http.HttpHost">
<constructor-arg value="${http.proxy.host}" />
<constructor-arg value="${http.proxy.port}" />
</bean>


<!-- See http://stackoverflow.com/questions/20340470/how-to-spring-ioc-and-httpclient-4-3-1-closeablehttpclient
for configuring the HttpRequestBuilder in spring -->
<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig" factory-method="custom">
<property name="proxy" ref="proxy" />
<property name="socketTimeout" value="50000" />
<property name="connectTimeout" value="50000" />
</bean>

<bean id="requestConfig" factory-bean="requestConfigBuilder" factory-method="build" />

<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder" factory-method="create">
<property name="defaultRequestConfig" ref="requestConfig" />
</bean>

<bean id="httpClient" factory-bean="httpClientBuilder" factory-method="build" />

我需要使用代理来访问该服务。此外,网络服务需要用户名和密码。我已多次验证代理和凭据详细信息是否有效

用于调用此网络服务的代码是

public boolean getItems() {

StringBuilder soapMessage = FileUtil.readFileContent(BASE_CONFIG_LOCATION + GET_ITEMS_XML_TEMPLTE);
StreamSource source = new StreamSource(new StringReader(soapMessage.toString()));
StreamResult result = new StreamResult(System.out);

String endpoint = baseURL + FIND_SERVICE_ENDPOINT;

boolean webserviceResult = webServiceTemplate.sendSourceAndReceiveToResult(endpoint,source, result);


return webserviceResult;
}

当我调用此代码时 - 我收到以下异常

 org.springframework.ws.client.WebServiceIOException: I/O error: null; nested exception is org.apache.http.client.ClientProtocolException
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:543)
at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:492)
at org.springframework.ws.client.core.WebServiceTemplate.sendSourceAndReceiveToResult(WebServiceTemplate.java:436)
at org.springframework.ws.client.core.WebServiceTemplate.sendSourceAndReceiveToResult(WebServiceTemplate.java:421)
at com.test.api.WebServiceImpl.getChecklist(WebServiceImpl.java:69)
at com.test.api.WebServiceRealCallsTest.testGetChecklist_Success(WebServiceRealCallsTest.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at org.springframework.ws.transport.http.HttpComponentsConnection.onSendAfterWrite(HttpComponentsConnection.java:119)
at org.springframework.ws.transport.AbstractWebServiceConnection.send(AbstractWebServiceConnection.java:47)
at org.springframework.ws.client.core.WebServiceTemplate.sendRequest(WebServiceTemplate.java:622)
at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:585)
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:537)
... 35 more
Caused by: org.apache.http.ProtocolException: Content-Length header already present
at org.apache.http.protocol.RequestContent.process(RequestContent.java:96)
at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:132)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:182)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
... 43 more

任何人都可以提供有关我需要做什么才能启动并运行此服务的指导吗?也许我的配置不正确?

谢谢

################ 更新

如果我使用以下代码 - 我的网络服务调用成功返回然而,我宁愿在 Spring 做所有这些,并且 httpClient getParams 方法也已被弃用

    public boolean getItems() {

HttpComponentsMessageSender sender = (HttpComponentsMessageSender) webServiceTemplate.getMessageSenders()[0];
sender.getHttpClient().getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxy);

StringBuilder soapMessage = FileUtil.readFileContent(BASE_CONFIG_LOCATION + GET_ITEMS_XML_TEMPLTE);
StreamSource source = new StreamSource(new StringReader(soapMessage.toString()));
StreamResult result = new StreamResult(System.out);

String endpoint = baseURL + FIND_SERVICE_ENDPOINT;

boolean webserviceResult = webServiceTemplate.sendSourceAndReceiveToResult(endpoint,source, result);

return webserviceResult;
}
#### 格林威治标准时间下午 1 点更新

我注意到,如果我在 xml 中为消息发送者指定 httpClient,则使用 org.apache.http.impl.client.InternalHttpClient 类

如果我不指定 httpClient - 使用 org.apache.http.impl.client.DefaultHttpClient 类

还有另一个有趣的点,在 org.springframework.ws.transport.http.HttpComponentsMessageSender 类中

/**
* Create a new instance of the {@code HttpClientMessageSender} with a default {@link HttpClient} that uses a
* default {@link org.apache.http.impl.conn.PoolingClientConnectionManager}.
*/
public HttpComponentsMessageSender() {
org.apache.http.impl.client.DefaultHttpClient defaultClient =
new org.apache.http.impl.client.DefaultHttpClient(new org.apache.http.impl.conn.PoolingClientConnectionManager());
defaultClient.addRequestInterceptor(new RemoveSoapHeadersInterceptor(), 0);

this.httpClient = defaultClient;
setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS);
setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
}

/**
* Create a new instance of the {@code HttpClientMessageSender} with the given
* {@link HttpClient} instance.
* <p>
* This constructor does not change the given {@code HttpClient} in any way. As such,
* it does not set timeouts, nor does it
* {@linkplain org.apache.http.impl.client.DefaultHttpClient#addRequestInterceptor(org.apache.http.HttpRequestInterceptor) add}
* the {@link RemoveSoapHeadersInterceptor}.
*
* @param httpClient the HttpClient instance to use for this sender
*/
public HttpComponentsMessageSender(HttpClient httpClient) {
Assert.notNull(httpClient, "httpClient must not be null");
this.httpClient = httpClient;
}

正如您所看到的 - 对于 DefaultClient - 它调用一个方法来删除soapHeaders,但对于 HttpClient 则不然。我查看了这个方法,并考虑添加一个拦截器来完成相同的工作,但 HttpRequest 在任何拦截器方法上都不可用

关于我可以从这里继续哪里有什么想法吗?

最佳答案

以防其他人遇到这种情况。诀窍是将拦截器添加到自定义客户端。

CloseableHttpClient httpClient = HttpClients.custom()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setSSLContext(ContextUtil.getTrustAllCertsContext())
.addInterceptorFirst(new HttpComponentsMessageSender.RemoveSoapHeadersInterceptor())
.build();

关于java - Spring WebService - 无法调用需要使用用户名和密码代理的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32519542/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com