Simple perl program to retrieve content from http web server using LWP and HTTP perl packages
This simple program on execution ask for url , on giving the url it sends the http request to the particular server and get the response.
#!/usr/local/bin/perl -w
use strict;
use HTTP::Status;
use HTTP::Response;
use LWP::UserAgent;
use URI::URL;
sub sendandreceive( ) {
my ($method, $path) = @_;
# Create a User Agent object
my $ua = new LWP::UserAgent;
$ua->agent("hcat/1.0");
# Ask the User Agent object to request a URL.
# Results go into the response object (HTTP::Reponse).
my $request = new HTTP::Request($method,$path);
my $response = $ua->request($request);
# Parse/convert the response object for "easier reading"
my $code=$response->code;
my $desc = HTTP::Status::status_message($code);
my $headers=$response->headers_as_string;
my $body = $response->content;
$body = $response->error_as_HTML if ($response->is_error);
return ($code, $desc, $headers, $body);
}
print "Enter the url \n";
my $url=
chomp $url;
my ($code, $desc, $headers, $body)=&sendandreceive('GET', $url);
#you can print and see the variables $code $desc $headers $body
0 Responses to Simple perl program to retrieve content from http web server using LWP and HTTP perl packages
Something to say?