use LWP::FileHandle; lwpopen URL, $method, $url, $query; #### lwpopen URL, GET => $url, 'this=that'; lwpopen URL, GET => "$url?this=that"; # can append QS to URL lwpopen URL, POST => $url, [ this => 'that' ]; lwpopen URL, POST => $url, { this => 'that' }; #### use LWP::FileHandle; lwpopen JAPHY, GET => 'http://www.crusoe.net/~jeffp/'; while () { print; } lwpclose JAPHY; #### package LWP::FileHandle; use IO::Socket; use URI::Escape; use Socket (); use Carp; use strict; use vars qw( @ISA @EXPORT $HEADERS ); require Exporter; @ISA = qw( Exporter ); @EXPORT = qw( lwpopen lwpclose ); $HEADERS = 1; my $CRLF = $Socket::CRLF; sub lwpopen (*@) { my ($fh,$mode,$url,$qs1) = @_; my ($host,$path,$qs2) = $url =~ m!(?:http://)?([^/]+)([^?]*)(.*)!; my ($query,$socket,$obj); $mode = uc $mode; croak "HTTP mode must be 'GET' or 'POST'" if $mode ne 'GET' and $mode ne 'POST'; if (UNIVERSAL::isa($qs1, 'ARRAY')) { for (my $i = 0; $i < @$qs1; $i += 2) { $query .= '&' if length $query; $query .= join '=', uri_escape($qs1->[$i]), uri_escape($qs1->[$i+1]); } } elsif (UNIVERSAL::isa($qs1, 'HASH')) { while (my ($k,$v) = each %$qs1) { $query .= '&' if length $query; $query .= join '=', uri_escape($k), uri_escape($v); } } elsif ($qs1 and not ref $qs1) { $query = $qs1 } elsif ($qs1) { croak "HTTP request must be array ref, hash ref, or string" } $query .= '&' if length $query and length $qs2; $query .= $qs2; $query = "?$query" if length $query; $path ||= '/'; $host .= ':80' if $host !~ /:\d+$/; $socket = IO::Socket::INET->new($host); { no strict 'refs'; tie *$fh, 'LWP::FileHandle::Tie', $socket, $host, $path, $query, $mode; } return $socket ? 1 : 0; } sub lwpclose (*) { no strict 'refs'; untie(*{ $_[0] }); } package LWP::FileHandle::Tie; sub TIEHANDLE { my ($class,$socket,$host,$path,$query,$mode) = @_; bless { SOCKET => $socket, READFROM => 0, PATH => $path, QUERY => $query, MODE => $mode, }, $class; } sub READLINE { my $socket = $_[0]{SOCKET}; if (!$_[0]{READFROM}++) { my ($path,$query) = @{$_[0]}{qw( PATH QUERY )}; if ($_[0]{MODE} eq 'GET') { $socket->print("GET $path$query HTTP/1.0$CRLF$CRLF"); } else { my $enctype = "application/x-www-form-urlencoded"; my $len = length $query; $socket->print("POST $path HTTP/1.0$CRLF"); $socket->print("Content-type: $enctype$CRLF"); $socket->print("Content-length: $len$CRLF$CRLF"); $socket->print($query); } if (!$LWP::FileHandle::HEADERS) { while (<$socket>) { last if $_ eq $CRLF } } } <$socket>; } sub DESTROY { $_[0]{SOCKET}->close; }