How to use Perl LWP to log into a web application?

I would like to write a script to enter the web application, and then move on to other parts of the application:

use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
use Data::Dumper;

$ua = LWP::UserAgent->new(keep_alive=>1);

my $req = POST "http://example.com:5002/index.php",
[ user_name     => 'username',
  user_password => "password",
  module        => 'Users',
  action        => 'Authenticate',
  return_module => 'Users',
  return_action => 'Login',
];

my $res = $ua->request($req);
print Dumper(\$res);
if ( $res->is_success ) {
    print $res->as_string;
}

When I try to execute this code, I cannot enter the application. The returned HTTP status code is 302, which was found, but without data.

If I send a username / password with all the necessary things, it should return the application home page and maintain a real-time connection to move other parts of the application.

+5
source share
3 answers

You can use WWW :: Mechanize for this purpose:

, . , . , , . URL-, , .

+15

, LWP :

push @{ $ua->requests_redirectable }, 'POST';

, WWW::Mechanize?

+9

LWP - , , . , :

  • ,
  • You may also need to enable cookies: $ ua-> cookie_jar ({});

Hope this helps

+1
source

All Articles