gpt4 book ai didi

java - 使用 Apache Camel 访问正确的 URL 时出现响应代码 302

转载 作者:太空宇宙 更新时间:2023-11-04 13:08:11 28 4
gpt4 key购买 nike

我正在使用 Apache Camel 访问 Web 服务,但在调用 .NET Web 服务时遇到了奇怪的问题。

这是Camel发送的POST请求:

ID: 1
Address: http://<domainName>/<pathToService>.asmx
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], breadcrumbId=[ID-someId-1449849054155-0-1], SOAPAction=["http://<someNamespace>/<SoapActionName>"]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>/soap:Body></soap:Envelope>

这就是响应:

ID: 1
Response-Code: 302
Encoding: UTF-8
Content-Type: text/html; charset=utf-8
Headers: {Cache-Control=[private], Content-Length=[223], content-type=[text/html; charset=utf-8], Location=[/<somePath>/CustomError.htm?aspxerrorpath=/<pathToService>.asmx], Persistent-Auth=[true], Server=[Microsoft-IIS/7.5], X-AspNet-Version=[4.0.30319], X-Powered-By=[ASP.NET], X-UA-Compatible=[IE=7]}
Payload: <html><head><title>Object moved</title></head></html>

当我在浏览器中请求“http://domainName/pathToService”时,我看到了正确的答案。

当我请求“http://domainName/pathToService.asmx?wsdl”时,我将获取 wsdl 文件。

如果我将 SoapActionName 更改为不正确的值,我会得到

"java.lang.IllegalArgumentException: Can't find the BindingOperationInfo with operation name ..." 

当然,这也是正确的行为。

我认为我需要调整 Camel 以使其能够与 .NET Web 服务一起使用。

但是如何呢?

Camel 配置(全部在 Spring DSL 中):

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://camel.apache.org/schema/cxf"
xmlns:http="http://cxf.apache.org/transports/http/configuration"
xmlns:sec="http://cxf.apache.org/configuration/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd">

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>

<bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>

<bean id="exceptionHandler" class="...ExceptionHandler"/>

<cxf:cxfEndpoint id="wsEndpoint"
address="${ws.host}/service.asmx"
serviceClass="pathToServiceClass"
loggingFeatureEnabled="true"
endpointName="endpointName">
<cxf:outInterceptors>
<ref bean="loggingOutInterceptor"/>
</cxf:outInterceptors>
<cxf:inInterceptors>
<ref bean="loggingInInterceptor"/>
</cxf:inInterceptors>
<cxf:properties>
<entry key="dataFormat" value="POJO"/>
</cxf:properties>
</cxf:cxfEndpoint>

<http:conduit name="{serviceClass}endpointName.http-conduit">
<http:authorization>
<sec:UserName>domain\username</sec:UserName>
<sec:Password>password</sec:Password>
<sec:AuthorizationType>NTLM</sec:AuthorizationType>
</http:authorization>
</http:conduit>

<camelContext xmlns="http://camel.apache.org/schema/spring">

<route id="mainRoute">
<from uri="file://inbox"/>
<setHeader headerName="operationName">
<constant>AcquireTicket</constant>
</setHeader>
<to uri="cxf:bean:wsEndpoint" pattern="InOut"/>
<onException>
<exception>java.lang.Exception</exception>
<process ref="exceptionHandler"/>
</onException>
<to uri="log:mylog?Level=INFO"/>
</route>
</camelContext>

我使用 JUnit 调用 Camel:

    @Test
public void testWebservice() throws Exception {
...
forming request
...

context.getRouteDefinition("mainRoute")
.adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith("direct:in");
}
});
context.start();


ProducerTemplate template = context.createProducerTemplate();
template.sendBody("direct:in",request);
}

我尝试在没有 Camel 的情况下使用 Apache HttpClient 的请求 Web 服务:

    DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getAuthSchemes().register(AuthPolicy.NTLM,new JCIFSNTLMSchemeFactory());
httpClient.getAuthSchemes().register(AuthPolicy.SPNEGO,new JCIFSNTLMSchemeFactory());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
NTCredentials ntCredentials = new NTCredentials(USERNAME, PASSWORD,
InetAddress.getLocalHost().getHostName(), DOMAIN);
HttpHost target = new HttpHost(HOST,80,"http");
credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort(),
AuthScope.ANY_REALM, "NTLM"), ntCredentials);
httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, ntCredentials);

HttpPost post = new HttpPost(URL);

post.addHeader("SOAPAction", SOAP_ACTION);
post.addHeader("Content-Type",CONTENT_TYPE);
post.addHeader("Accept-Encoding",ACCEPT_ENCODING);
HttpEntity httpEntity = new StringEntity(getShortText());
post.setEntity(httpEntity);

HttpResponse response = httpClient.execute(post);

这是 Camel 创建的最后一个 POST 请求(3 个 NTLM 请求中的 3 个):

    POST /<pathToService>.asmx HTTP/1.1
Content-Type: text/xml; charset=UTF-8
Accept: */*
SOAPAction:<someNamespace>/<SoapActionName>
User-Agent: Apache CXF 3.1.4
Cache-Control: no-cache
Pragma: no-cache
Host: localhost:8013
Connection: keep-alive
Content-Length: 269
Authorization: NTLM <Token>

Apache HttpClient 使用上述代码创建了最后一个(3 个 NTLM 请求中的 3 个)POST 请求:

    POST /<pathToService>.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
Accept-Encoding: gzip,deflate
SOAPAction:<someNamespace>/<SoapActionName>
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

Host: localhost:8013
Connection: Keep-Alive
Content-Length: 269
Authorization: NTLM <Token>

这两个请求看起来与我相同,但 IIS 对 Camel 请求的响应是 HTTP 302,IIS 对 Apache HttpCLient 的响应是 HTTP 200。

主机是 localhost:8013 因为我使用监听器来分析 HTTP 流量,该监听器将所有请求转发到远程服务器。

最佳答案

因为您的浏览器可以遵循位置 header 的 302 重定向。

您得到的响应是 HTTP 302,它是基于位置 header 的重定向。所以你需要告诉Camel跟随302并从location header中获取URI。

关于java - 使用 Apache Camel 访问正确的 URL 时出现响应代码 302,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34228294/

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