ajay.awachar has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I'm trying some web automation wherein I have to login to an web application and check the contents of page which I can get after logging in. I tried to do it with LWP::UserAgent and WWW::Mechanize modules.

Using LWP::UserAgent I'm getting status code 302 Moved Temporarily and contents of the Login page instead of the Application Page which we should get after logging in. Following is the code snippet.

#!/usr/bin/perl use strict; use LWP::UserAgent; use LWP::Simple; ## set URL my $url = "http://abc.toronto.xyz.com:65342"; my $ua = LWP::UserAgent->new(); my $res = $ua->post($url, {username => 'rkadm', password => 'rkadm'} ) +; $res->content_type('text/html'); my $response = $res->decoded_content( charset => 'none' ); print "Response Type = ".$res->content_type; print "\nResponse Status ".$res->status_line; ## view print "\nResponse = ".$res->content;

Using WWW::Mechanize after form submission, I'm getting the same contents of Login page instead of application page.

#!/usr/bin/perl -w use strict; use WWW::Mechanize; my $username = "rkadm"; my $password = "rkadm"; my $login = "Login"; my $url = "http://abc.toronto.xyz.com:65342"; # login to your photobucket.com account my $mech = WWW::Mechanize->new(); $mech->get($url); $mech->submit_form( form_name => 'loginForm', fields => { username => $username, password => $password +} ); print "Response = \n ".$mech->content( format => 'text' ); if( $mech->success() ) { print "\n Successful hit.....\n"; } print "Response = \n ".$mech->content( format => 'text' );

It should have returned the contents of application page after logging in. Any idea on this.

Thanks,

Ajay

  • Comment on Problem with fetching contents of page after login form submission using POST: LWP::UserAgent and WWW::Mechanize
  • Select or Download Code

Replies are listed 'Best First'.
Re: Problem with fetching contents of page after login form submission using POST: LWP::UserAgent and WWW::Mechanize
by Corion (Patriarch) on Feb 28, 2010 at 15:33 UTC

    Look at what goes on when you do the same via the browser, for example by using Wireshark or Mozilla Live HTTP Headers. Maybe it's a Javascript redirect, or there is an error message somewhere in the page that you're missing or something else.

      Thanks Corion.

      After observing request through Mozilla HTTP Live Headers I observed that it was causing failure because URL has an internal redirect. After making necessary changes to the URL, it worked perfectly fine for me. Your advise really helped me to resolve the issue.

      Thanks,

      Ajay
Re: Problem with fetching contents of page after login form submission using POST: LWP::UserAgent and WWW::Mechanize
by almut (Canon) on Feb 28, 2010 at 15:36 UTC