Java – jax-ws client: maintaining sessions / cookies in multiple services

I use NetBeans to automatically create web service clients from WSDL files This is good, except that the web services I use require me to pass the custom cookie in the HTTP header to each web service to prove my identity

I use a web service called utility to obtain my authorization This will set up a cookie that needs to be provided to any web service in all subsequent calls

This can be done by adding javax. XML to the bindingprovider on the web service's port xml. ws. session. Set maintain to true This is very useful for subsequent calls to the methods in Utility WebService. The problem is that it only maintains the session / cookie for that single web service I need it, too

I need to pass the cookie to a separate WebService named history. How do I implement it? Is it feasible to have a super service class, utilities and history that can extend and share the same session state?

Solution

I found a solution

After making a call, you can use this method to get the response header:

((BindingProvider)port).getResponseContext().get(MessageContext.HTTP_RESPONSE_HEADERS);

Find the set cookie header and store its value

Then, before your next request (in any WebService), you can set the cookie Title:

((BindingProvider)port).getRequestContext().put(
            MessageContext.HTTP_Request_HEADERS,Collections.singletonMap("Cookie",Collections.singletonList(cookieValue)
            )
        );
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>