我正在尝试在node.js中运行一系列请求。我被建议使用async-waterfall。我需要登录远程vbulletin安装并搜索帖子。
waterfall([
function(callback){
var options = {
jar: true,
form: {
'vb_login_password': '',
'vb_login_username': mtfConfig.user,
's': '',
'do': 'login',
'vb_login_md5password': crypto.createHash('md5').update(mtfConfig.password).digest("hex"),
'vb_login_md5password_utf': crypto.createHash('md5').update(mtfConfig.password).digest("hex"),
'submit.x' :13,
'submit.y' :9
},
//formData: form,
url: targetBaseURL+'/login.php',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36'
},
followAllRedirects:true,
proxy: 'http://127.0.0.1:8888'
}
request.post(options, function(err, resp, body)
{
//console.log(res)
$ = cheerio.load(body);
var href = $('div.panel a').attr('href');
callback(null, href, this.jar);
});
},
function(href, jar, callback) {
console.log('second callback called');
request.get({
jar:jar,
url:href,
followAllRedirects: true,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36'
},
proxy: 'http://127.0.0.1:8888'
}, function(err, resp,body){
$ = cheerio.load(body);
console.log($('div.signup h2').text());
callback(null, request.jar);
});
},
function (jar, callback) {
console.log('third callback - search.php')
request.get({
jar:jar,
url:targetBaseURL+'/search.php',
followAllRedirects: true,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36'
},
proxy: 'http://127.0.0.1:8888'
}, function(err, resp,body){
$ = cheerio.load(body);
console.log(jar);
});
}
], done);
我已经尝试从第一个请求传递cookie jar但是当我到达search.php时,我没有登录。如何在请求和链接回调中维护会话cookie?
答案 0 :(得分:0)
我找到了答案here
工作代码(仅限第一个功能)
function(callback){
var jar = request.jar();
var options = {
jar: jar,
form: {
'vb_login_password': '',
'vb_login_username': mtfConfig.user,
's': '',
'do': 'login',
'vb_login_md5password': crypto.createHash('md5').update(mtfConfig.password).digest("hex"),
'vb_login_md5password_utf': crypto.createHash('md5').update(mtfConfig.password).digest("hex"),
'submit.x' :13,
'submit.y' :9
},
//formData: form,
url: targetBaseURL+'/login.php',
headers: {
'User-Agent': userAgent
},
followAllRedirects:true,
proxy: 'http://127.0.0.1:8888'
}
request.post(options, function(err, resp, body)
{
//console.log(res)
$ = cheerio.load(body);
var href = $('div.panel a').attr('href');
callback(null, href,jar);
console.log(jar.cookies); //jar now contains the cookies we need
});
},