1

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

我想与肥皂服务器通信。为了维护会话,服务器向我发送 set-cookie 响应标头:'ASP.NET_SessionID=55....hrc; path/; HttpOnly'带有 Login 方法响应。我想在注销方法请求中将此 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 标头?

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

4

2 回答 2

2

我有同样的问题,发现它可以用标题来完成:

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)
  })
})
于 2018-12-20T13:42:21.257 回答
0

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

request库正在使用的soap库支持 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 });
于 2021-06-10T14:31:46.870 回答