天天看點

解決 CXF 發送soap請求時因為缺少content-length 傳回 "411 Length require"

錯誤資訊記錄:

2013-3-8 14:41:48 org.apache.cxf.phase.PhaseInterceptorChain doIntercept

資訊: Interceptor has thrown exception, unwinding now

org.apache.cxf.interceptor.Fault: Response was of unexpected text/html ContentType.  Incoming portion of HTML stream: <html>

<head><title>411 Length Required</title></head>

<body bgcolor="white">

<center><h1>411 Length Required</h1></center>

<hr><center>nginx/1.2.2</center>

</body>

</html>

at org.apache.cxf.interceptor.StaxInInterceptor.handleMessage(StaxInInterceptor.java:73)

at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:226)

at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:619)

at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:2054)

at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1939)

at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1865)

at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:66)

at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:599)

at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)

at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:226)

at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:449)

at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:279)

at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:231)

at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)

at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:117)

at $Proxy28.getProjectList(Unknown Source)

at com.TegstMain.main(TegstMain.java:26)

通過打出用戶端cxf的封包資訊:

lighttpd 傳回"411 Length require"字樣的錯誤。同時,在lighttpd的err日志裡面,有如下的錯誤資訊 

2007-09-27 16:17:39: (request.c.1110) POST-request, but content-length 

missing -> 411

結果确定原因是cxf用戶端調用時缺少了content-length屬性,需要進行如下配置:

Client client = ClientProxy.getClient(greeter); 

HTTPConduit http = (HTTPConduit) client.getConduit(); 

HTTPClientPolicy httpClientPolicy = http.getClient(); 

httpClientPolicy.setAllowChunking(false);

如果是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>

注:第一個設定用戶端屬性驗證通過,第二個測試失敗,也許我的配置不正确。