当WebService返回代码403时,如何在CXF中获取HTTP响应主体?

问题描述 投票:7回答:3

我正在尝试使用Apache的CXF库为WebService开发客户端应用程序。在这个特定的服务器实现中,当请求中缺少一些数据时(例如,某人的ID号),它返回HTTP代码403(禁止),但是包含应用程序特定错误详细信息的响应主体作为Soap Fault。

举个例子,这是我使用SoapUI收集的响应:

Response in the SoapUI 正如您在突出显示的文本中所看到的,此请求中有一个响应正文。


Now I need to retrieve the response body from within my application. I tried using interceptors in different phases such as SEND_ENDING and POST_PROTOCOL, but can't seem to find it within the Message parameter given to the handleMessage() method.

我错过了什么?

这是我得到的异常和堆栈跟踪:

org.apache.cxf.interceptor.Fault: Could not send Message.
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:67)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:531)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:440)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:355)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:313)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:140)
    at com.sun.proxy.$Proxy36.arquivo(Unknown Source)
    at br.com.dgsistemas.TesteWS.main(TesteWS.java:133)
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '403: Forbidden' when communicating with https://www.wsrestrito.caixa.gov.br/siies/WsSolicitacao
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.doProcessResponseCode(HTTPConduit.java:1620)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1627)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1572)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1373)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:673)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:63)
    ... 9 more

谢谢!

java soap cxf
3个回答
1
投票

你有两个独立的问题。

首先,您必须删除分块消息。 http://cxf.apache.org/docs/client-http-transport-including-ssl-support.html#ClientHTTPTransport(includingSSLsupport)-ANoteAboutChunking

在调用端点之前,必须禁用分块通信:

   HTTPConduit conduit = (HTTPConduit) client.getConduit();
   HTTPClientPolicy policy = new HTTPClientPolicy();


   // Chunking is by default enabled in CXF webservices so we have to disable it.     
   policy.setAllowChunking(false);
   conduit.setClient(policy); // update HTTP client's conduit

其次,我认为您必须删除BOM。您可以在以下维基百科注释中看到它是什么:https://en.wikipedia.org/wiki/Byte_order_mark

如果你想删除BOM,请检查:Byte order mark screws up file reading in Java

注意1:分块消息取决于服务器设置,服务器可能会忽略您的请求设置。

注意2:如果编写流拦截器,则可以处理BOM和chunked消息。分块消息没有Content-Length标头,虽然实际长度小于预期,但您必须等待来自服务器的更多消息。


1
投票

您应该能够扩展AbstractSoapInterceptor,在Phase.MARSHAL阶段注册它,并在handleMessage覆盖中提取消息。

使用SoapMessage.getExchange()。getInMessage()或.getInFaultMessage()从SOAP响应中提取消息。


0
投票

您是否尝试使用LoggingInInterceptor(See description),或者扩展它并覆盖handleMessage方法。它可用于监视所有SOAP IN消息

Example for usage can be found here

© www.soinside.com 2019 - 2024. All rights reserved.