[英]Node.js Post Request - Keep Session Alive
Hi guys (Happy new year btw :) )! 大家好(新年快乐:))! I'm giving a try to Node.js and Express.js, and, i'm facing some difficulties with the processing of several requests.
我正在尝试Node.js和Express.js,并且在处理多个请求时遇到了一些困难。
Here's the problem : I need to retrieve data from an external website. 问题是:我需要从外部网站检索数据。 To do so, I have to be logged in and I have a fake/zombie account available.
为此,我必须登录并且我有一个伪造/僵尸帐户。 What I am trying to do is to first send a POST request with my username and password, and then, sending a GET request to access a specific content.
我想做的是先发送带有我的用户名和密码的POST请求,然后发送GET请求以访问特定内容。 The problem is that, even if the POST request is successfully executed, when I'm doing the GET request, I am no longer logged in. I was doing the same thing with the Gem Mechanize in Ruby but, it should probably do some implicit stuff that made the whole process work.
问题是,即使POST请求成功执行,当我执行GET请求时,我也不再登录。我在Ruby中使用Gem Mechanize做同样的事情,但是,它可能应该做一些隐式的操作。使整个过程正常工作的东西。
To make requests, I am using the request module ( https://github.com/request/request ). 为了发出请求,我使用了请求模块( https://github.com/request/request )。 And, here is the code :
并且,这是代码:
var zombie = require('request').defaults({
headers: { connection: "keep-alive" },
pool: {maxSockets: 1},
});
var connect = function(req, res, next){
zombie.post({
url: "target.com",
form: {
user: "zombie",
pass: "password"}
}, function(err, response, body){
req.connected = true;
next();
});
}
var information = function(req, res, next){
if(req.connected){
zombie.get({
url: "target.com",
qs: {
page: "targetPage",
search: req.params.search}
}, function(err, response, body){
res.send(body);
})
}
}
And, from a controller, I'm trying to connect, then to access information. 而且,我正在尝试从控制器进行连接,然后访问信息。
var router = require('express').Router();
var zombie = require('./../middleware/Zombie');
router.get('/search/:search', zombie.connect, zombie.information);
Most likely the server is sending back a Set-Cookie
and it's getting ignored (the default behavior for the request
module). 服务器很可能会发送回
Set-Cookie
并且它会被忽略( request
模块的默认行为)。 To fix that, you need to npm install tough-cookie
and then set up the jar
config option . 要解决此问题,您需要
npm install tough-cookie
,然后设置jar
config选项 。 There are some examples that show various cookie jar usage scenarios. 有一些示例显示了各种Cookie罐的使用情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.