Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending HTTP request in Perl

Tags:

http

windows

perl

How can I send a request like this in Perl on Windows?

GET /index.html HTTP/1.1
Host: www.example.org
Cookie: test=quest
like image 696
OOO ''MMM'' Avatar asked Dec 13 '22 11:12

OOO ''MMM''


2 Answers

You can do this using sockets:

use IO::Socket;
my $sock = new IO::Socket::INET (
                                 PeerAddr => 'www.example.org',
                                 PeerPort => '80',
                                 Proto => 'tcp',
                                );
die "Could not create socket: $!\n" unless $sock;
print $sock "GET /index.html HTTP/1.0\r\n";
print $sock "Host: www.example.org\r\n";
print $sock "Cookie: test=quest\r\n\r\n";
print while <$sock>;
close($sock);

but you might want to consider using LWP (libwww-perl) instead:

use LWP::UserAgent;
$ua = LWP::UserAgent->new;

$req = HTTP::Request->new(GET => 'http://www.example.org/index.html');
$req->header('Cookie' => 'test=quest');

# send request
$res = $ua->request($req);

# check the outcome
if ($res->is_success) { print $res->decoded_content }
else { print "Error: " . $res->status_line . "\n" }

You might try reading the LWP cookbook for an introduction to LWP.

like image 119
nandhp Avatar answered Dec 15 '22 01:12

nandhp


LWP::UserAgent is the normal starting point. You can pass in an HTTP::Cookies object manually if you want to set up specific cookie values in advance.

like image 34
Quentin Avatar answered Dec 15 '22 01:12

Quentin