Auto login web form

2019-08-29 11:19发布

问题:

I have the next problem:

I have a device that it has an xml page (http://IP_device/counters.xml), I want to monitor this file for extract some information. The problem comes because for access to this file, previously I must login into a form (http://IP_device/frameCmd_Login.htm) like this:

<form method="get"  action="/Action_Login" onsubmit="return MD5HASH()">
<font color="#000000" align="center">Please Enter Password</font>
<input size="21" type="password" value="" name="LOGINPASSWORD" id="PD" />
<input name="submit" type="submit" value="LOGIN" />

For that I need to log in into the form before (http://IP_device) and then download the xml file.

I've tried with the LWP, URL modules but I don't know how to do this. I'm a newbie with perl. The perl script I've tried is:

#!/usr/bin/perl
use LWP::UserAgent;

my $ua = new LWP::UserAgent;
my $req = new HTTP::Request(GET => 'http://IP_device/frameCmd_Login.htm'); 
$req->authorization_basic("password123");

my $res = $ua->request($req);

if ($res->is_success) 
{
    my $file = $res->content;
    print $file;
} 
else 
{
    die $res->status_line;
}

Anyone knows how to achieve this issue? Anyone can help me?

回答1:

authorization_basic is good for standard HTTP authentication, but Web forms are something different. Remove that method call and learn how Web forms function.

Following is the simplest change to your code to make it work. Note we are targeting the resource in the form's action attribute directly.

my $u = URI->new('http://IP_device/Action_Login');
$u->query_form(LOGINPASSWORD => 'password123');
my $req = HTTP::Request->new(GET => $u->as_string);