2014-03-04 54 views
0

我试图在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); 

我试过第一次请求通过传递饼干罐,但是当我到达的search.php,我没有登录。我怎样才能保持整个请求和链接回调会话cookie?

回答

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 


     }); 

    },