To install LWP on Ubuntu or Debian:
$ sudo apt-get install libwww-perl
To install LWP on CentOS, Fedora or RHEL:
$ sudo yum install perl-libwww-perl.noarch
HTTP GET Perl example
use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $server_endpoint = "http://192.168.1.1:8000/service"; # set custom HTTP request header fields my $req = HTTP::Request->new(GET => $server_endpoint); $req->header('content-type' => 'application/json'); $req->header('x-auth-token' => 'kfksj48sdfj4jd9d'); my $resp = $ua->request($req); if ($resp->is_success) { my $message = $resp->decoded_content; print "Received reply: $message\n"; } else { print "HTTP GET error code: ", $resp->code, "\n"; print "HTTP GET error message: ", $resp->message, "\n"; }
HTTP POST Perl example
use LWP::UserAgent;my $ua = LWP::UserAgent->new;
my $url = "http://192.168.1.1:8000/service";
my $resp = $ua->post( $url, { 'term' => $md5 } );
if ($resp->is_success) {
my $message = $resp->decoded_content;
print "Received reply: $message\n";
}
else {
print "HTTP POST error code: ", $resp->code, "\n";
print "HTTP POST error message: ", $resp->message, "\n";
}
沒有留言:
張貼留言