【问题标题】:getting http session cookies in spring-ws soap web service client在 spring-ws 肥皂 Web 服务客户端中获取 http 会话 cookie
【发布时间】:2016-04-05 21:23:23
【问题描述】:

我有一个用 Spring-WS 编写的 SOAP Web 服务客户端。为了响应登录请求,我连接的 Web 服务在 HTTP 响应标头中返回一个会话 cookie,其中包含一个令牌。对服务的每次后续调用都需要带有令牌的 cookie。如何从响应中获取会话 cookie,然后在后续调用服务时将其添加到 HTTP 标头?

总结一下我的问题,您如何使用 Spring-WS 提取和注入 cookie。

这是一段类似于我用来调用 Web 服务的代码的 sn-p:

    MySessionLogon api = new MySessionLogon();
    api.setUsername(username);
    api.setPassword(password);

    MySessionLogonResponse response = (MySessionLogonResponse) getWebServiceTemplate()
            .marshalSendAndReceive(
                    "http://myserver/soapservice.asmx",
                    api,
                    new SoapActionCallback("http://www.myserver.com/MySession_Logon"));

    return response;

我已经在互联网上搜索并阅读了很多似乎接近我需要的东西,但是我还没有看到一个很好的方法来获得我需要使用的 cookie 的价值。任何帮助将不胜感激,在此先感谢。

【问题讨论】:

    标签: web-services cookies soap http-headers spring-ws


    【解决方案1】:

    你检查WebServiceMessageExtractor 了吗?

    你可以这样做

    MySessionLogonResponse response = (MySessionLogonResponse) getWebServiceTemplate()
                .sendAndReceive(
                        "http://myserver/soapservice.asmx",
                        api,
                        new SoapActionCallback("http://www.myserver.com/MySession_Logon"),
    new WebServiceMessageExtractor<ResponseAndHeader>() {
        public ResponseAndHeader extractData(WebServiceMessage message) throws IOException {
        TransportContext context = TransportContextHolder.getTransportContext();
        HttpComponentsConnection con = (HttpComponentsConnection) context.getConnection();
        httpResponse httpResponse = con.getHttpResponse();
        //use the method getHeaders from to retrieve the data you want    
        });
    

    【讨论】:

    • 感谢 VirtualTroll 的回复。请注意,marshalSendAndReceive 不接受您的回复中显示的四个参数。我猜我需要使用 WebServiceTemplate 提供的其他 SendAndReceive 方法之一。
    • 更新了我的答案。您是否能够使用该解决方案解决您的问题?
    • 抱歉耽搁了这么久,但我通过不同的路线解决了我的问题。我实现了一个类似于 stackoverflow.com/questions/25189129/… 的解决方案,其中有价值的输入来自:ankeetmaini.wordpress.com/2013/03/04/…> 和 blog.vinodsingh.com/2008/11/…> 等。再次感谢您的意见,即使我没有那样做。 @VirtualTroll
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 2021-11-16
    • 2023-03-20
    • 2019-01-27
    相关资源
    最近更新 更多