WWW Mechanize – 401 Authorization Required

Here is a small script on how to login to a .htaccess protected page. Suppose that URL we want to login to is:


and here are credentials required to login:

username: www
password: mechanize

The perl script to fetch the content of this page will look like this:


use WWW::Mechanize;
my $mech = WWW::Mechanize->new( autocheck => 1 );

$url = 'http://www.example.com/';
$mech->credentials( 'www' => 'mechanize' );
    $mech->get( $url );
    print $mech->content();