gpt4 book ai didi

java - WebService返回403码时如何获取CXF中的HTTP响应体?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:33:28 25 4
gpt4 key购买 nike

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

例如,这是我使用 SoapUI 收集的响应:

Response in the 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

谢谢!

最佳答案

你有两个独立的问题。

首先,您必须删除分块消息。 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

NOTE1: The chunked messages depends on server settings, the server may ignore your request setting.

NOTE2: You can handle BOM and chunked message both if you write a stream interceptor. The chunked messages don't have a Content-Length header, and while actual length lesser than the expected, you have to wait more message from server.

关于java - WebService返回403码时如何获取CXF中的HTTP响应体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54527468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com