JAX-WS client: maintain session/cookies across multiple services

馋奶兔 提交于 2019-11-27 20:46:50

I've found a solution.

You can get response headers using this after you've made the call:

((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 header:

((BindingProvider)port).getRequestContext().put(
            MessageContext.HTTP_REQUEST_HEADERS,
                Collections.singletonMap("Cookie", Collections.singletonList(cookieValue)
            )
        );

Just commenting because solution above didn't work for me. I got UnsupportedOperationException. I believe issue was caused because singletonMap doesn't allow changes. xml headers were also needed, so I set those first.

Map<String, List<String>> headers= CastUtils.cast((Map)port.getRequestContext().get("javax.xml.ws.http.request.headers"));
if (headers == null) {
    headers = new HashMap<String, List<String>>();
    port.getRequestContext().put("javax.xml.ws.http.request.headers", headers);
}

headers.put("Cookie", Collections.singletonList(cookieValue));
((BindingProvider)port).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, headers); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!