LWP (short for "Library for WWW in Perl") is a popular group of Perl modules for accessing data on the Web. Like most Perl module-distributions, each of LWP's component modules comes with documentation that is a complete reference to its interface. However, there are so many modules in LWP that it's hard to know where to look for information on doing even the simplest things.
Document: http://www.perl.com/pub/2002/08/20/perlandlwp.html
#!/usr/bin/perl -w
# For supporting HTTPS Protocol:
# [root@centos ~]# yum install perl-Net-SSLeay perl-Crypt-SSLeay
# [root@ubuntu ~]# apt-get install libcrypt-ssleay-perl libnet-ssleay-perl
# [root@ubuntu ~]# apt-get install libcrypt-ssleay-perl libnet-ssleay-perl
use HTTP::Request;
use HTTP::Cookies;
use LWP::UserAgent;
$get_content = get('http://facebook.com/');
use HTTP::Cookies;
use LWP::UserAgent;
$get_content = get('http://facebook.com/');
$post_content = post('http://m.facebook.com/login.php',
"email=user@mail.com&pass=password&login=Login");
sub get {
my $url = $_[0];
my $cookie = HTTP::Cookies->new(file => "cookies.log", autosave=>1,);
my $ua = LWP::UserAgent->new(agent => "Mozilla/5.0");
$ua->cookie_jar($cookie);
$ua->timeout(15);
my $req = HTTP::Request->new(GET => $url);
my $res = $ua->request($req);
if ($res->is_error) { print $res->status_line."\n"; }
return $res->content;
}
sub post {
my $url = $_[0];
my $data = $_[1];
my $cookie = HTTP::Cookies->new(file => "cookies.log", autosave=>1,);
my $ua = LWP::UserAgent->new(agent => "Mozilla/5.0");
$ua->cookie_jar($cookie);
$ua->timeout(15);
push @{$ua->requests_redirectable}, 'POST';
my $req = HTTP::Request->new(POST => $url);
$req->content_type('application/x-www-form-urlencoded');
$req->content("$data");
my $res = $ua->request($req);
if ($res->is_error) { print $res->status_line."\n"; }
return $res->content;
}
sub get {
my $url = $_[0];
my $cookie = HTTP::Cookies->new(file => "cookies.log", autosave=>1,);
my $ua = LWP::UserAgent->new(agent => "Mozilla/5.0");
$ua->cookie_jar($cookie);
$ua->timeout(15);
my $req = HTTP::Request->new(GET => $url);
my $res = $ua->request($req);
if ($res->is_error) { print $res->status_line."\n"; }
return $res->content;
}
sub post {
my $url = $_[0];
my $data = $_[1];
my $cookie = HTTP::Cookies->new(file => "cookies.log", autosave=>1,);
my $ua = LWP::UserAgent->new(agent => "Mozilla/5.0");
$ua->cookie_jar($cookie);
$ua->timeout(15);
push @{$ua->requests_redirectable}, 'POST';
my $req = HTTP::Request->new(POST => $url);
$req->content_type('application/x-www-form-urlencoded');
$req->content("$data");
my $res = $ua->request($req);
if ($res->is_error) { print $res->status_line."\n"; }
return $res->content;
}
Happy coding :)
0 komentar: on "GET & POST with PERL"
Poskan Komentar