Loggin on site with LWP and Perl

A little inexperienced programmer trying to write a program to enter my courses website and upload all the content (lectures, homework, etc.). Obviously, this is a password protected site, so I have to give it away. I understand LWP :: UserAgent very well and liked it and I need to use credentials. I can’t figure out how to get to the next page. I can proceed to registration, but how will perl get the result of my login?

Sample code (I pulled out log-info):

use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
my $url = 'login URL';
$ua -> credentials(
  $url,
  '',
  'user',
  'pass'
);
my $response = $ua ->get($url);
print $response->content; 

the content from the response is the same content as mine, as if I were not transmitting any credentials. Obviously, I missed something ...

Oh, one more thing on which my own course site does not have a unique URL as far as I know.

+3
3

, WWW::Mechanize, LWP:: UserAgent, , , - cookie.

+5

credentials, HTTP basic auth, " ", .

, cookie_jar URI- , .

+2
#!/usr/bin/perl

use LWP::UserAgent;
use HTTP::Cookies;

my  $ua=LWP::UserAgent->new(timeout => 20);
    $ua->agent('Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.1.8) Gecko/20100202 MRA 5.5 (build 02842) Firefox/3.5.8');
    $ua->requests_redirectable(0);

my  $cook = HTTP::Cookies->new;
    $ua->cookie_jar($cook);

print = requester('http://urlexample/login.php', 'login=yourlogin&password=pass' )->as_string;

sub requester
{
    my $type = 'GET';
    if($_[1]){$type = 'POST'}   
    my $req = HTTP::Request->new($type => $_[0]);
    $req->content_type('application/x-www-form-urlencoded; charset=UTF-8');
    if($_[1]){$req->content($_[1])}
    my $res = $ua->request($req);
    return $res;
}
0
source

All Articles