简体   繁体   English

如何在异步瀑布调用之间维护请求会话?

[英]How can I maintain a request session across async-waterfall calls?

I'm trying to run a series of requests in node.js. 我正在尝试在node.js中运行一系列请求。 I was advised to use async-waterfall . 建议我使用async-waterfall I need to log into a remote vbulletin install and do a search for posts. 我需要登录到远程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);

I've tried passing the cookie jar through from the first request but when I reach search.php, I am not logged in. How I can maintain the session cookies across requests and chained callbacks? 我尝试从第一个请求传递cookie罐,但是当我到达search.php时,我没有登录。如何维护请求和链接回调之间的会话cookie?

I found the answer here 我在这里找到答案

Working code (first function only) 工作代码(仅第一个功能)

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


        });

    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM