WWW:Mechanize HTTPS File Download

I was trying to download a governmental DB..but of course you had to login so after several hours I came up with this;

NOTE: This is being done an a windows machine.

#!c:\\perl\\bin
use strict;
use WWW::Mechanize;
use HTTP::Cookies;
my $outfile = "out.htm";
my $url = "ENTER_YOUR_URL_TO_LOGIN";
my $username = 'ENTER_YOUR_USERNAME';
my $password = 'ENTER_YOUR_PASSWORD';
my $mech = WWW::Mechanize->new();
#Debug
#$mech->add_handler("request_send", sub { shift->dump; return });
#$mech->add_handler("response_done", sub { shift->dump; return });
#END Debug
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($url);
$mech->form_name('ENTER_NAME_OF_FORM');
$mech->set_visible( $username, $password ) ;
$mech->click('ENTER_BUTTON_NAME');
my $output_page = $mech->content();
my $download = 'ENTER_URL_OF_FILE_TO_DOWNLOAD';
$mech->get($download);
if ( ! open( FOUT, ">download.zip" ) ) {
    die( "Could not create file: $!" );
}
binmode( FOUT ); # required for Windows
print( FOUT $mech->response->content() );
close( FOUT );

I could never find good documentation on WWW:Mechanize, I had to piece different examples together to get this to work. So hopefully this will save another poor soul a couple hours worth of work.

Your IP Address is:
50.16.166.175

Leave a Reply

Human Verification: