Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have been trying to log into a page and getting HTML response. I have used the following code:
#!/usr/bin/perl use Time::localtime; use LWP::Simple; # Create a user agent object use LWP::UserAgent; $ua = LWP::UserAgent->new; $req = HTTP::Request->new(GET => 'http://www.xyz.com/Protected/Track.a +spx'); $ua->credentials( 'www.ats.ca:80', 'http://www.xyz.com/Login.aspx', 'user' => 'password' ); # send request $res = $ua->request($req); # check the outcome if ($res->is_success) { print "\n\n\n\n\nSuccessful\n\n\n\n\n"; print $res->content; } else { #print $res->content; print "Error: " . $res->status_line . "\n"; }
The problem is when I run this script, HTML page of Login is returned whereas I want to get HTML page of http://www.xyz.com/Protected/Track.aspx. How could I get this page?
Thanks
Akhil

2005-03-24 Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: Log into a screen and getting a response
by sh1tn (Priest) on Mar 24, 2005 at 11:59 UTC
    use WWW::Mechanize; my $username = 'user'; my $password = 'pass'; my $url = 'http://www.xyz.com/Login.aspx'; my $mech = WWW::Mechanize->new(autocheck => 1); $mech->get( $url ); $mech->form_name("login form name"); $mech->field("username", $username); $mech->field("password", $password); $mech->click("login button/submit name"); $mech->content() =~ /sm.th. from Track.aspx/ and print "logged in\n" __END__
    Update: you may want to use mech-dump - like this:
    mech-dump "http://some.web-site.com/login.html"
    in order to see the input fields.


      Thanks for the code. I have downloaded the Mechanize 1.12 version from net. When I executed the command "perl Makefile.PL", I encountered the following warnings:
      Warning: prerequisite HTML::Form 1.038 not found at /opt/perl/lib/5.6.1/ExtUtils/MakeMaker.pm line 343.
      Warning: prerequisite HTML::TokeParser 2.28 not found at /opt/perl/lib/5.6.1/ExtUtils/MakeMaker.pm line 343.
      Warning: prerequisite HTTP::Request 1.3 not found at /opt/perl/lib/5.6.1/ExtUtils/MakeMaker.pm line 343.
      Warning: prerequisite LWP 5.76 not found at /opt/perl/lib/5.6.1/ExtUtils/MakeMaker.pm line 343.
      Warning: prerequisite LWP::UserAgent 2.024 not found at /opt/perl/lib/5.6.1/ExtUtils/MakeMaker.pm line 343.
      Warning: prerequisite Test::More failed to load: Can't locate Test/More.pm in @INC (@INC contains: /opt/perl/lib/5.6.1/PA-RISC1.1-thread-multi /opt/perl/lib/5.6.1 /opt/perl/lib/site_perl/5.6.1/PA-RISC1.1-thread-multi /opt/perl/lib/site_perl/5.6.1 /opt/perl/lib/site_perl .) at (eval 33) line 3.
      Warning: prerequisite URI 1.25 not found at /opt/perl/lib/5.6.1/ExtUtils/MakeMaker.pm line 343.

      What should be done to eliminate these warnings?
      Akhil
Re: Log into a screen and getting a response
by johnnywang (Priest) on Mar 24, 2005 at 18:25 UTC
    sh1tn already gave you the solution, just want to point out that the credentials you were using would work if the site is using the basic authentication (the popup window you see in some sites), rather than a login page. In the latter case, you need to scrap that page directly as sh1tn did (look at the html source to get the field names correctly.)