Perl – Using HTTP::Lite
I recently had to use the HTTP::Lite perl module and thought I would share an example of how to use it. HTTP::Lite was is available on www.cpan.org; its very lightweight and has no dependancies other than Socket & Fcntl which included with almost all Perl implementations by default.
Based on the examples by the HTTP::Lite author Roy Hooper <rhooper@thetoybox.org>
PERL:
-
#!perl
-
-
use HTTP::Lite;
-
-
-
#
-
# Get and print out the headers and body of the Google homepage
-
#
-
-
$http = new HTTP::Lite;
-
-
$req = $http->request("http://www.google.co.uk/")
-
-
# Print the reconstructed status line
-
-
# Get the Headers & Body
-
@headers = $http->headers_array();
-
$body = $http->body();
-
-
# Display each Header
-
foreach $header (@headers) {
-
}
-
print "\n\n";
-
-
# Display the Body
-
-
-
-
#
-
# Execute a Google Search using the POST method
-
#
-
-
$http = new HTTP::Lite;
-
-
# Post variables
-
%vars = (
-
"hl" => "en",
-
"q" => "cpan",
-
"btnG" => "Search",
-
"meta" => ""
-
);
-
$http->prepare_post(\%vars);
-
-
$req = $http->request("http://www.google.co.uk/search")
-
-
# Print the reconstructed status line
-
-
# Get the Headers & Body
-
@headers = $http->headers_array();
-
$body = $http->body();
-
-
# Display each Header
-
foreach $header (@headers) {
-
}
-
print "\n\n";
-
-
# Display the Body
Google doesnt support queries by POST methods anymore, so the response to the second request here will be a "501 Not Implemented".
No Comments so far
Leave a comment
Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>