简体   繁体   English

在 spring-ws 肥皂 Web 服务客户端中获取 http 会话 cookie

[英]getting http session cookies in spring-ws soap web service client

I have a SOAP web service client written with Spring-WS.我有一个用 Spring-WS 编写的 SOAP Web 服务客户端。 In response to a logon request, the web service I connect to returns a session cookie in the HTTP response header with a token in it.作为对登录请求的响应,我连接到的 Web 服务在 HTTP 响应标头中返回一个会话 cookie,其中包含一个令牌。 Each subsequent call to the service requires that cookie with the token.对服务的每次后续调用都需要带有令牌的 cookie。 How can I get the session cookie from the response and then add it to the HTTP header on subsequent calls to the service?如何从响应中获取会话 cookie,然后在后续调用服务时将其添加到 HTTP 标头中?

To summarize what I'm asking, how do you extract and inject cookies with Spring-WS.总结一下我要问的问题,您如何使用 Spring-WS 提取和注入 cookie。

Here is a snippet of code similar to what I am using to call into the web service:这是一段类似于我用来调用 Web 服务的代码片段:

    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;

I have searched the internet and read a lot of things that seem close to what I need, but I haven't seen a good way to get the value of the cookies I need to use.我已经在互联网上搜索并阅读了很多似乎接近我需要的东西,但是我还没有看到一种获得我需要使用的 cookie 的价值的好方法。 Any help would be appreciated, thanks in advance.任何帮助将不胜感激,提前致谢。

Did you check WebServiceMessageExtractor ?你检查了WebServiceMessageExtractor吗?

You could do the following您可以执行以下操作

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    
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM