重定向,验证并获取NodeJS

时间:2016-10-16 14:58:50

标签: node.js http url-redirection

我有一个要求,我必须使用NodeJS中的URL获取页面。

问题

网址重定向到“登录”页面,我们必须使用凭据进行身份验证。 一旦我们提供了凭据,它就会重定向到我想要获取的实际页面。 这就是它在浏览器中的工作方式。

有没有办法让最后一页作为输出?

    var https = require('follow-redirects').https;
    var options = {
      host: 'myURL',
      port: 443,
      path: 'myPath'
    };

    https.get(options, function(res) {
     res.setEncoding('utf8');
        res.on('data', function(chunk){
            console.log(chunk);
        });
    });

我正在获取200的登录页面。现在,如何使用我拥有的凭据自动验证自己并进入我真正想要的页面?

1 个答案:

答案 0 :(得分:0)

最后使用casperJS达到了要求。感谢@Dave Newton的评论。

   var casper = require('casper').create(); 

    casper.on("remote.message", function (msg) {
        console.log(msg);
    });

    casper.start("https://url.com/path", function () {
        //Enter Credentials
        this.evaluate(function () {
            console.log("filling inputs");

            var usernameInput = document.getElementById("accountname");
            usernameInput.value = "username";

            var passwordInput = document.getElementById("accountpassword");
            passwordInput.value = "password";
        });
        this.click("#continueFieldbutton");
        this.echo("login button was submitted");
    });
    casper.then(function () {
        var js = this.evaluate(function() {
            return document; 
        }); 
        this.echo(js.all[0].outerHTML); 
        this.capture('example.png');
    });
    casper.run();