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

Lea*_*gem 7 java soap cxf

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

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

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


现在我需要从我的应用程序中检索响应主体.我尝试在不同的阶段使用拦截器,例如SEND_ENDINGPOST_PROTOCOL,但似乎无法在Message给予该handleMessage()方法的参数中找到它.

我错过了什么?

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

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
Run Code Online (Sandbox Code Playgroud)

谢谢!

小智 0

你有两个独立的问题。

首先,您必须删除分块的消息。 http://cxf.apache.org/docs/client-http-transport-include-ssl-support.html#ClientHTTPTransport(includesSSLsupport)-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
Run Code Online (Sandbox Code Playgroud)

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

如果你想删除 BOM,请检查以下内容: Byte order mark Screws up file Reading in Java

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

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