JAX-WS 客户端:跨多个服务维护会话/cookie [英] JAX-WS client: maintain session/cookies across multiple services
问题描述
我正在使用 Netbeans 基于 WSDL 文件自动创建 Web 服务客户端.这很有效,除了我使用的网络服务要求我在 HTTP 标头中将自定义 cookie 传递给每个网络服务以证明我的身份.
I'm using Netbeans to automatically create webservice clients based off WSDL files. This works well, except the webservices I'm using require that I pass in a custom cookie in the HTTP header to every webservice in order to prove my identity.
我使用一个名为 Utility
的网络服务来获得我的授权.这会设置一个 cookie,需要在对任何网络服务的所有后续调用中提供该 cookie.
I use one webservice named Utility
to get my authorization. This sets a cookie which needs to be provided in all subsequent calls to any webservice.
这可以通过在 web 服务端口的 BindingProvider 上将 javax.xml.ws.session.maintain
设置为 true
来实现.这对于后续调用 Utility
网络服务中的方法非常有用.问题是这仅维护该单个 Web 服务的会话/cookie.其他人也需要它.
This can be accomplished by setting javax.xml.ws.session.maintain
to true
on the BindingProvider of the port for the webservice. This works great for subsequent calls to the methods in the Utility
webservice.
The problem is that this only maintains the session/cookie for that single webservice. I need it across others as well.
我需要将 cookie 传递给名为 History
的 单独 网络服务,我该如何实现?拥有一个Utility 和History 都可以扩展并共享相同会话状态的超级Service 类是否可行?
I need the cookie passed in to a separate webservice named History
How can I accomplish this? Is it feasible to have a super Service class which both Utility and History could extend and share the same session state with?
推荐答案
我找到了解决方案.
您可以在拨打电话后使用它获取响应标头:
You can get response headers using this after you've made the call:
((BindingProvider)port).getResponseContext().get(MessageContext.HTTP_RESPONSE_HEADERS);
找到 Set-Cookie
标头并存储其值.
Find the Set-Cookie
header and store its value.
然后在您的下一个请求(在任何网络服务中)之前,您可以设置 Cookie 标头:
Then before your next request (in any webservice) you can set the Cookie header:
((BindingProvider)port).getRequestContext().put(
MessageContext.HTTP_REQUEST_HEADERS,
Collections.singletonMap("Cookie", Collections.singletonList(cookieValue)
)
);
这篇关于JAX-WS 客户端:跨多个服务维护会话/cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!