如何使用jax-ws客户端获取RAW消息?

时间:2015-07-13 17:59:46

标签: java xml web-services soap

我正在尝试从Web服务而不是解析的soap版本获取原始XML响应。

我使用的是使用org.apache.cxf.tools.wsdlto.WSDLToJava生成的jax-ws客户端。问题是有时web服务返回HTML错误,SOAP解析失败,我在日志中得到以下内容:

 Response was of unexpected text/html ContentType.  Incoming portion of HTML stream: System.ArgumentException: Chunked encoding must be set via the Sen
dChunked property.
Parameter name: value
   at System.Net.HttpWebRequest.set_TransferEncoding(String value)
   at MultiversionRouter.Routers.BaseRequestRouter.RouteRequest(HttpContext app, AsyncCallback callback, WebResponse& errorResponse)
   at MultiversionRouter.RouteProviderHandler.beginRequest(Object sender, EventArgs e, AsyncCallback callback, Object state)
2015-07-02 08:58:51,054 [Thread-63063        ] javax.xml.ws.soap.SOAPFaultException: Response was of unexpected text/html ContentType.  Incoming portion of HTML stream: System.ArgumentException: Chunked encoding must be set via the SendChunked property.
Parameter name: value
   at System.Net.HttpWebRequest.set_TransferEncoding(String value)
   at MultiversionRouter.Routers.BaseRequestRouter.RouteRequest(HttpContext app, AsyncCallback callback, WebResponse& errorResponse)
   at MultiversionRouter.RouteProviderHandler.beginRequest(Object sender, EventArgs e, AsyncCallback callback, Object state)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:157)
    at com.sun.proxy.$Proxy85.updateServiceTicketViaManagedId(Unknown Source)
    at com.nable.libpsa.ConnectWiseProxy.updateTicket(ConnectWiseProxy.java:538)
    at com.nable.libpsa.ConnectWiseProxy.run(ConnectWiseProxy.java:1591)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.cxf.interceptor.Fault: Response was of unexpected text/html ContentType.  Incoming portion of HTML stream: System.ArgumentException: Chunked encoding must be set via the SendChunked property.
Parameter name: value
   at System.Net.HttpWebRequest.set_TransferEncoding(String value)
   at MultiversionRouter.Routers.BaseRequestRouter.RouteRequest(HttpContext app, AsyncCallback callback, WebResponse& errorResponse)
   at MultiversionRouter.RouteProviderHandler.beginRequest(Object sender, EventArgs e, AsyncCallback callback, Object state)
    at org.apache.cxf.interceptor.StaxInInterceptor.handleMessage(StaxInInterceptor.java:84)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
    at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:835)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1614)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1504)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1310)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:628)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:565)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:474)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:377)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:330)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:135)
    ... 4 more

我尝试过使用javax.xml.ws.handler.soap.SOAPHandler,但是当我们收到此错误时它永远不会达到这一点,它在成功调用时完全正常。

1 个答案:

答案 0 :(得分:0)

请使用:

<强> javax.xml.ws.handler.soap.SOAPHandler

您可以使用SOAPMessageContext,方法getMessage()获取消息,并将消息转换为String。

示例代码:

public static String getXmlMessage(SOAPMessage message) throws Exception
   {
         ByteArrayOutputStream os = new ByteArrayOutputStream();
         message.writeTo(os);
         final String encoding = (String) message.getProperty(SOAPMessage.CHARACTER_SET_ENCODING);
         if (encoding == null)
         {
             return new String(os.toByteArray());
         }
         else
         {
            return new String(os.toByteArray(), encoding);    
         }
   }