【问题标题】:Send cookie using node-soap使用 node-soap 发送 cookie
【发布时间】:2018-03-02 15:58:24
【问题描述】:

直到现在我还没有使用肥皂。我正在使用 node-soap 库 (https://github.com/vpulim/node-soap)。

我想与肥皂服务器通信。为了维护会话,服务器向我发送了 set-cookie 响应标头:'ASP.NET_SessionID=55....hrc; path/; HttpOnly' 以及 Login 方法响应。我想在 Logout 方法请求中发回这个 cookie。

我尝试: client.addHttpHeader('Cookie', 'ASP.NET_SessionID=55....hrc');

但在后续:

client.LogoutAsync({})
    .then(result => {console.log(result);})
    .catch(error => {console.log(error);});

我收到一个错误:

events.js:183 throw er; //Unhandled 'error' event

这是怎么回事,我怎样才能发回 Cookie 标头?

附:如果没有 cookie,它会按预期工作,返回我 Logout: false,这意味着发生错误是因为它无法识别会话(我想)。

【问题讨论】:

    标签: node.js http soap node-soap


    【解决方案1】:

    我遇到了同样的问题,发现可以用标题来解决:

    soap.createClientAsync(this.url, this.options).then(client => {
      client.addHttpHeader('Cookie', 'ASP.NET_SessionId=' + this.sessionId)
      client.GetIdentity((err, results) => {
        if (err) reject(err)
        resolve(results)
      })
    })
    

    【讨论】:

      【解决方案2】:

      我们刚刚遇到了同样的问题并找到了一个简单的解决方案。

      soap 库正在使用的 request 库支持 cookie jar 的概念。

      您可以通过将jar 选项添加到所有请求来启用它。例如:

      client.LoginAsync({}, { jar: true });
      client.LogoutAsync({}, { jar: true });
      

      这会全局存储 cookie。

      如果您想在本地存储 cookie(例如,每个会话),您可以使用自定义 cookie jar 对象,该对象仅用于特定范围,例如:

      const request = require('request');
      
      const myJar = request.jar();
      client.LoginAsync({}, { jar: myJar });
      client.LogoutAsync({}, { jar: myJar });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-07
        • 1970-01-01
        • 2020-11-28
        • 2019-12-17
        • 1970-01-01
        • 2011-11-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多