Get http and post errors with WWW::Mechanize

Posted on 2009-05-18
Last Modified: 2012-05-07
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 Access Forbidden at testin 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.

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 = "";

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

$mech->agent_alias( 'Windows IE 6' );




my $output_page = $mech->content();

open(OUTFILE, ">$outfilewelcome");

print OUTFILE "$output_page";


my $welcomepagedumper;

my $outd;

$welcomepagedumper = "datetime-serverurl-1welcomepage-dumper.txt";

open(OUTFILE, ">$welcomepagedumper");

print OUTFILE Dumper($mech);


# now fill out the logon form


$mech->field(ffusername => $username);

$mech->field(ffpassword => $password);


my $output_page = $mech->content();

open(OUTFILE, ">$outfilelogonpage");

print OUTFILE "$output_page";


my $logonpagedumper;

my $outd;

$logonpagedumper = "datetime-serverurl-2logonpage-dumper.txt";

open(OUTFILE, ">$logonpagedumper");

print OUTFILE Dumper($mech);


my $stringocookie;

$stringocookie =  $mech->cookie_jar->as_string;

print "here is the string of cookies :\n\n" . $stringocookie;

Open in new window

Question by:markpalinux
  • 3
  • 2
LVL 39

Expert Comment

ID: 24413089
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.


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

#OR like this:



if(!$mech->success) {

    die "Error getting $url: " . $mech->status . "\n";


Open in new window

LVL 15

Author Comment

ID: 24414246

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.



Open in new window

LVL 39

Accepted Solution

Adam314 earned 500 total points
ID: 24415861
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.
LVL 15

Author Comment

ID: 24417829

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

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


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

both of those work well.

Thanks for you answers.
LVL 15

Author Closing Comment

ID: 31602453

Thanks for your help.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Input from stdin for perl 6 118
collecting information 2 140
PHP equivalent of opening a com object 5 69
Linux rename files by reordering dates in the file name 6 33
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now