Get http and post errors with WWW::Mechanize

I am trying to write a script to test login at a web page, I will schedule this every x minutes.

At times the web server does strange things and I want my script to capture those problems and report them.

what I did was disable POST at the http server and here is what I get:
Error POSTing http://127.0.0.1/testforperl/login.asp: Access Forbidden at testin
g6b.pl line 44

and the script stops, I want a way to get the http protocol error and put that into a text file.

Here my sample code, in the pod for Mechanize is seems like you would be able to capture the error with carp but I am not sure how to put that into my code.

Thanks,
Mark
use strict;
use WWW::Mechanize;
use HTTP::Cookies;
use Data::Dumper;
 
my $username;
my $password; 
 
$username = "username10";
$password = "password10";
 
my $outfilewelcome = "datetime-serverurl-1welcomepage-pageout.html";
my $outfilelogonpage = "datetime-serverurl-2logonpage-pageout.html";
 
 
my $url = "http://127.0.0.1/testforperl/default.asp";
my $mech = WWW::Mechanize->new();
 
 
$mech->agent_alias( 'Windows IE 6' );
 
$mech->cookie_jar(HTTP::Cookies->new());
$mech->timeout(10);
$mech->get($url);
 
my $output_page = $mech->content();
open(OUTFILE, ">$outfilewelcome");
print OUTFILE "$output_page";
close(OUTFILE);
 
my $welcomepagedumper;
my $outd;
$welcomepagedumper = "datetime-serverurl-1welcomepage-dumper.txt";
open(OUTFILE, ">$welcomepagedumper");
print OUTFILE Dumper($mech);
close(OUTFILE);
 
 
# now fill out the logon form
$mech->form_name('logonForm');
$mech->field(ffusername => $username);
$mech->field(ffpassword => $password);
$mech->click();
 
 
my $output_page = $mech->content();
open(OUTFILE, ">$outfilelogonpage");
print OUTFILE "$output_page";
close(OUTFILE);
 
my $logonpagedumper;
my $outd;
$logonpagedumper = "datetime-serverurl-2logonpage-dumper.txt";
open(OUTFILE, ">$logonpagedumper");
print OUTFILE Dumper($mech);
close(OUTFILE);
 
my $stringocookie;
$stringocookie =  $mech->cookie_jar->as_string;
 
print "here is the string of cookies :\n\n" . $stringocookie;

Open in new window

LVL 15
markpalinuxAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Adam314Connect With a Mentor Commented:
Is there only 1 button on the form?  If there is more than 1 button, you need to specify the name of the button in the call to click.  It might be easier to use the submit method instead.
0
 
Adam314Commented:
You can get the status of the last GET or POST using the status method.  If you want to check if the operation was successful or not, you can use the success method.
...
$mech->get($url);
print "Status is: " . $mech->status . "\n";
 
 
 
#OR like this:
...
$mech->get($url);
if(!$mech->success) {
    die "Error getting $url: " . $mech->status . "\n";
}

Open in new window

0
 
markpalinuxAuthor Commented:



print "Status is: " . $mech->status . "\n";

It prints "Status is : 200", only if I use it before $mech->click(); for the logon form.

Its like the perl script stops at that $mech->click(); line rather then going to the next line.

Mark

$mech->click();

Open in new window

0
 
markpalinuxAuthor Commented:


I jumped into perl irc on freeenode and got two suggestions:

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

and

eval{ $mech->click(); };



both of those work well.


Thanks for you answers.
0
 
markpalinuxAuthor Commented:


Thanks for your help.
0
All Courses

From novice to tech pro — start learning today.