【发布时间】:2012-05-16 10:51:13
【问题描述】:
我正在使用插件WWW::Scripter(WWW::Mechanize 的子类)来验证我主机的登录页面。他们在登录页面上使用Ruby 和一些JavaScript 功能,所以我不能只使用LWP::Agent 模块。代码如下:
#!/usr/bin/env perl
use strict;
use warnings;
use diagnostics;
use LWP::Debug qw(+);
use LWP::ConnCache;
use WWW::Scripter;
my $url = 'https://control.vp.net/login';
my $username = 'example@example.com';
my $password = 'example';
my $w = WWW::Scripter->new(keep_alive => 1) or die "error1: $!\n";
$w->conn_cache(LWP::ConnCache->new);
$w->use_plugin('JavaScript') or die "error2: $!\n";
$w->credentials($url, undef, $username, $password) or die "error3: $!\n";
$w->get($url) or die "error4: $!\n";
print $w->content() or die "error5: $!\n";
我遇到以下错误:
Uncaught exception from user code:
error3
我已经用谷歌搜索了好几个小时,我觉得我现在真的需要你的帮助。我将不胜感激任何有助于理解为什么我无法进行身份验证的帮助。如果重要的话,我的 Perl 版本是 Ubuntu 11 上的 5.10.1。
谢谢。
更新
我已将代码中的一行更改为:
$w->credentials($username, $password) or die "error3: $!\n";
现在只得到白页。如果我启用诊断编译指示,则会出现一个相当普遍的错误:
Use of uninitialized value in subroutine entry at blib/lib/Net/SSLeay.pm
(autosplit into blib/lib/auto/Net/SSLeay/randomize.al) line 2227 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
【问题讨论】:
-
尝试使用
$w->credentials($username, $password),不使用$url, undef。WWW::Scripter类是WWW::Mechanize的子类,因此请参阅 search.cpan.org/~jesse/WWW-Mechanize-1.72/lib/WWW/Mechanize.pm 以获取credentials文档 -
感谢您的回复。我已经更新了我的帖子。您能帮忙解决白页问题吗?
-
您使用的
$url是否正确?暂时删除行$w->use_plugin('JavaScript') or die "error2: $!\n";以了解您是否因为该页面中的某些脚本而获得白页,或者仅仅是因为这是一个页面内容。 -
是的,这有帮助。现在,没有任何白页,只有简单的 html。我仍然无法进行身份验证。据我了解,我应该检查成功登录的 HTTP 标头并尝试在我的代码中执行相同操作?
-
您收到空白回复的原因可能有几十个。首先,尝试添加用户代理信息,使其看起来像一个真正的浏览器。我相信您正在获得状态 302,并且在您获得的响应中的 http 标头中有一些重定向位置,因此您必须继续使用这样的新 url。此外,您可能还需要添加 cookie 和referer,以保持会话等。祝您好运!
标签: perl mechanize www-mechanize