在我的perl脚本中,我想通过设置$mech->redirect_ok(0)来停止重定向页面;但我得到了以下错误:-无法对/usr/share/perl5/LWP/UserAgent.pm行563 中未定义的值调用方法"request"
perl程序如下所示,供您参考。
#!/usr/bin/perl -w
use utility;
use WWW::Mechanize;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
binmode(STDOUT, ":utf8");
my $mech = WWW::Mechanize->new( autocheck => 1 );
my $num_args = $#ARGV + 1;
if ($num_args != 2) {
print "nUsage: ./my_script.pl username passwordnn";
exit;
}
my $username = $ARGV[0];
my $password = $ARGV[1];
$username = main::trim ($username);
$password = main::trim ($password);
$mech->credentials( $username => $password );
$mech->redirect_ok(0);
$mech->get( '<home page of the web address>.jspa' );
print $mech->content();
请建议。。。。
redirect_ok
是LWP内部调用的回调函数,用于发现特定重定向是否可允许。它通过子类化LWP
和重载redirect_ok
来对请求和响应执行更复杂的测试。
该方法采用两个参数,一个HTTP::Request
和一个HTTP::Response
。您正在传递零作为HTTP::Request
,传递undef
作为HTTP::Response
。Zero作为用户代理request
方法的参数是无用的,因此程序崩溃。
我不清楚到底需要什么,但要禁用所有重定向,您应该使用requests_redirectable
方法,该方法需要一个对重定向有效的HTTP请求类型列表。默认情况下,它设置为
$mech->requests_redirectable([qw/ GET HEAD /]);
因此仅POST重定向将被拒绝。要防止重定向所有请求类型,请传递一个空列表,如以下
$mech->requests_redirectable([]);