node.js - 使用node.js请求模块获取带有FileCookieStore的页面

标签 node.js cookies request

我正在尝试使用 node.js 请求从我们使用的网络服务器获取页面。 我从上一个请求的结果中检索cookie, 现在尝试将它们作为请求参数发送到个人资料页面。

var request = require('request'),
fileCookieStore = require('tough-cookie-filestore');

var j = request.jar(new fileCookieStore("./cookie.json"));
var options = {
    url     : 'http://example.com/site/page.php?u=1234',
    jar     : j,
    headers : {
        "User-Agent":"user_agent"
    }
};

request(options, function(err,res,body) {
if(err){
    console.log(err);
    return;
    }
console.log('body');
});

cookie 文件看起来像这样:

[{
    "domain": "www.example.com",
    "expirationDate": 1482144640.897115,
    "hostOnly": true,
    "httpOnly": true,
    "name": "password",
    "path": "/",
    "secure": false,
    "session": false,
    "storeId": "0",
    "value": "8ff31b0edcf85b72b20469044dafc373",
    "id": 1
  },
  {
    "domain": "www.example.com",
    "hostOnly": true,
    "httpOnly": true,
    "name": "sessionhash",
    "path": "/",
    "secure": false,
    "session": true,
    "storeId": "0",
    "value": "13d0e4ff1bdeefbe118df4ad04c81a2e",
    "id": 2
  }]

而且没有运气。控制台日志中的正文显示我仍未登录。我该怎么做?

最佳答案

尝试传递 j 对象,即您在下一个请求中在 jar 方法中传递的对象。 我在工作中也做了类似的事情

来自请求网站:

jar - If true, remember cookies for future use (or define your custom cookie jar; see examples section)

https://github.com/request/request

关于node.js - 使用node.js请求模块获取带有FileCookieStore的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34381531/

相关文章:

javascript - 从 javascript 编辑点 cookie

Javascript正则表达式用双引号包裹不带引号的JSON值(不是键)

node.js - 当套接字与服务器断开连接时,如何从房间取消订阅套接字

javascript - azure函数不在本地运行

java - 如何在Fragment中调用requestWindowFeature

javascript - 请求模块 : how do I store the collected cookies in a variable?

javascript - 如何在 javascript 中访问请求内容正文中的查询参数?

javascript - Express webapp如何存储/访问用户设置

cookies - 如何在 Amazon S3 中禁用 cookie

php - JWT Cookie 存储不正确 - Laravel