使用node.js请求模块获取具有FileCookieStore的页面 [英] using node.js request module to get a page with FileCookieStore
问题描述
我想使用node.js请求从我们使用的网络服务器抓取网页。
我从上一个请求的结果中检索cookie,
,现在尝试将它们作为请求参数发送到配置文件页面。
I'm trying to use node.js request to fetch a page from a webserver we use. I retrieve the cookies from the results of the previous request, and now trying send them to the profile page as request argument.
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文件如下所示:
cookie file looks like that:
[{
"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
}]
body在控制台日志中说我仍然没有登录。我该怎么办?
and there's no luck. body in console log says that I'm still not logged in. How can I do that?
推荐答案
尝试传递j对象,您在下一个请求中传递的jar方法。
我在我的工作中做类似的事情
Try to pass j object, the one that you passed in jar method in your next request. I do something similar at my work
从请求网站:
jar - 如果为true,记住cookies以供将来使用(或定义您的自定义cookie 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请求模块获取具有FileCookieStore的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!