How can I keep WWW::Mechanize from following redirects?

家住魔仙堡 提交于 2019-12-05 01:12:44
Alexandr Ciornii

WWW::Mechanize is a subclass of LWP::UserAgent. So you can use any LWP::UserAgent methods.

my $mech = WWW::Mechanize->new();
$mech->requests_redirectable([]);

WWW::Mechanize is a subclass of LWP::UserAgent; you can set the max_redirect or requests_redirectable options in the constructor as you would with LWP::UserAgent.

You can use $agent->max_redirect( 0 );, like in this example:

#!/usr/bin/perl -w
use strict;

use WWW::Mechanize;

my $agent = WWW::Mechanize->new( 'autocheck' => 1, 'onerror' => undef, );
$agent->max_redirect( 0 );
$agent->get('http://www.depesz.com/test/redirect');
printf("Got HTTP/%s from %s.\n", $agent->response->code, $agent->uri);

$agent->max_redirect( 1 );
$agent->get('http://www.depesz.com/test/redirect');
printf("Got HTTP/%s from %s.\n", $agent->response->code, $agent->uri);

When running it prints:

Got HTTP/302 from http://www.depesz.com/test/redirect.
Got HTTP/200 from http://www.depesz.com/.

So, with max_redirect(0) - it clearly doesn't follow redirects.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!