CXF调用WebService报错: HTTP response ‘411: Length Required’
血的教训,今天碰到接口调用失败,找了很多资料,网上资料很少而且没有什么实际解决方案,此处特记录以供以后遗忘时使用。
使用CXF调用WebService的时候,接口地址使用域名,内网环境会出现如下错误:
javax.xml.ws.WebServiceException: Could not send Message.
...省略...
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '411: Length Required' when communicating with http://www.xxxx.com/xxxx/xxxxx/xxxx.asmx?wsdl
...省略...
经过测试,内网通过域名可直接访问该wsdl地址,但是通过CXF调用就会出现该错误。(不明所以)
- 解决方案:
将域名地址修改为IP地址即可。 - 案例:
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.getOutInterceptors().add(newLoggingOutInterceptor());
factoryBean.setServiceClass(xxxxxxService.class);
factoryBean.setAddress("http://www.xxxx.com/xxxx/xxxxx/xxxx.asmx?wsdl");
xxxxxxService impl = (xxxxxService) factoryBean.create();
String wsResultXml = impl.xxxxxxx(xml);
此时就可能出现:
HTTPException: HTTP response ‘411: Length Required’ when communicating with
将:factoryBean.setAddress(“http://www.xxxx.com/xxxx/xxxxx/xxxx.asmx?wsdl”);改为
factoryBean.setAddress(“http://192.168.1.123:321/xxxx/xxxxx/xxxx.asmx?wsdl”);即可
网上解决方案也一并发出来以供参考
1、cxf客户端调用时缺少了content-length属性,需要进行如下配置:
Client client = ClientProxy.getClient(greeter);
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = http.getClient();
httpClientPolicy.setAllowChunking(false);
2、修改cxf.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sec="http://cxf.apache.org/configuration/security"
xmlns:http="http://cxf.apache.org/transports/http/configuration"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xsi:schemaLocation="
http://cxf.apache.org/configuration/security
http://cxf.apache.org/schemas/configuration/security.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<http:conduit name="*.http-conduit">
<http:client AllowChunking="false">
</http:conduit>
</beans>
3、请求方式GET修改为POST,GET参数过长导致