【问题标题】:How could I store the cookie in local storage using Node request package?如何使用 Node 请求包将 cookie 存储在本地存储中?
【发布时间】:2015-08-05 06:17:10
【问题描述】:

我想模仿签署一些网站。我需要在应用程序出现恐慌时重新运行该应用程序,并输入验证码。我希望设置主管自动重新运行它并重用我得到的 cookie,但我发现请求的 Jar 没有一些方法,比如从其他服务器设置 cookie。我只能像文档一样设置我创建的cookie:

// `npm install --save tough-cookie` before this works
var j = request.jar()
var cookie = request.cookie('your_cookie_here')
j.setCookie(cookie, uri);
request({url: 'http://www.google.com', jar: j}, function () {
  request('http://images.google.com')
})

并获得像这样的cookie:

var j = request.jar() 
request({url: 'http://www.google.com', jar: j}, function () {
  var cookie_string = j.getCookieString(uri); // "key1=value1; key2=value2; ..."
  var cookies = j.getCookies(uri); 
  // [{key: 'key1', value: 'value1', domain: "www.google.com", ...}, ...]
})

如何重用从其他服务器获取的 cookie?

【问题讨论】:

  • 你找到解决办法了吗?

标签: node.js cookies


【解决方案1】:

https://github.com/request/request找到解决方案:

使用自定义 cookie 存储(例如 FileCookieStore 支持保存到 JSON 文件和从 JSON 文件恢复),将其作为参数传递 致request.jar()

var FileCookieStore = require('tough-cookie-filestore');
// NOTE - currently the 'cookies.json' file must already exist!
var j = request.jar(new FileCookieStore('cookies.json'));
request = request.defaults({ jar : j })
request('http://www.google.com', function() {
  request('http://images.google.com')
})

【讨论】:

    猜你喜欢
    • 2012-05-31
    • 1970-01-01
    • 2017-09-19
    • 2018-02-06
    • 2012-03-28
    • 2020-12-26
    • 2019-10-26
    • 2021-04-06
    • 2013-08-17
    相关资源
    最近更新 更多