Javascript PhantomJS 不发送身份验证标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10114925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:54:44  来源:igfitidea点击:

PhantomJS doesn't send authentication header

javascripthttpphantomjs

提问by Karl Barker

I'm trying to open a web page which requires HTTP authentication, in PhantomJS. My script is based off the loadspeed.js example:

我正在尝试在 PhantomJS 中打开一个需要 HTTP 身份验证的网页。我的脚本基于 loadspeed.js 示例:

var page = require('webpage').create(),
    t, address;
page.settings.userName = "user";
page.settings.password = "password";
if (phantom.args.length === 0) {
  console.log('Usage: loadspeed.js <some URL>');
  phantom.exit();
} else {
  t = Date.now();
  address = phantom.args[0];
  page.open(address, function (status) {
      if (status !== 'success') {
          console.log('FAIL to load the address');
      } else {
          t = Date.now() - t;
          console.log('Loading time ' + t + ' msec');
          page.render('page.jpg');
      }
      phantom.exit();
  });
}

I can see from the rendered page.jpg that I'm getting a 401 every time. I've also traced the HTTP session using Wireshark, which reveals that no authentication header is sent in the GET request to the given URL.

我可以从呈现的 page.jpg 中看到每次都收到 401。我还使用 Wireshark 跟踪了 HTTP 会话,这表明在给定 URL 的 GET 请求中没有发送任何身份验证标头。

What am I doing wrong here? I'm just getting started with PhantomJS but I've been searching all evening and not gotten far...

我在这里做错了什么?我刚刚开始使用 PhantomJS,但我整个晚上都在搜索并且没有走远......

回答by Darren Cook

PhantomJS (at least as of 1.9.0) has a bug with auth: it sends the request without the auth headers, and then only after it gets the 401 back does it do the request again but this time with the headers. (That is for GET; with POST it doesn't work at all.)

PhantomJS(至少从 1.9.0 开始)有一个 auth 错误:它发送没有 auth 标头的请求,然后只有在它返回 401 后才再次执行请求,但这次使用标头。(这是针对 GET 的;对于 POST,它根本不起作用。)

The workaround is simple, so instead of:

解决方法很简单,而不是:

page.settings.userName = 'username';
page.settings.password = 'password';

you can use:

您可以使用:

page.customHeaders={'Authorization': 'Basic '+btoa('username:password')};

(I just covered this in a blog post: http://darrendev.blogspot.jp/2013/04/phantomjs-post-auth-and-timeouts.html, and learnt that workaround on the PhantomJS mailing list from Igor Semenko.)

(我刚刚在博客文章中介绍了这一点:http: //darrendev.blogspot.jp/2013/04/phantomjs-post-auth-and-timeouts.html,并从 Igor Semenko 的 PhantomJS 邮件列表中了解到了该解决方法。)

回答by Dve

I dont think there is anything wrong with the script your using or phantomjs (at least in v1.5).

我认为您使用的脚本或 phantomjs(至少在 v1.5 中)没有任何问题。

If you try this script:

如果你尝试这个脚本:

var page = require('webpage').create(),
    system = require('system'),
    t, address;

page.settings.userName = 'test';
page.settings.password = 'test';

if (system.args.length === 1) {
    console.log('Usage: loadspeed.js <some URL>');
    phantom.exit();
} else {
    t = Date.now();
    address = system.args[1];
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('FAIL to load the address');
        } else {
            t = Date.now() - t;
            console.log('Page title is ' + page.evaluate(function () {
                return document.title;
            }));
            console.log('Loading time ' + t + ' msec');
        }
        phantom.exit();
    });
}

phantomjs loadspeed.js http://browserspy.dk/password-ok.php

phantomjs loadspeed.js http://browserspy.dk/password-ok.php

The auth is successful.

认证成功。