#!/usr/bin/perl -w # vim: set sw=4 ts=4 si et: # copyright: freeware, do with it whatever you want. No warranty of any kind. use strict; use Socket; if(!$ARGV[1] || $ARGV[0]!~/http:\/\//i){ print "USAGE: webclient-post.pl http://some.url/and/path \"key=value\&key1=value1\"\n"; exit(0); } my $host; my $port=80; my $path="/"; if ($ARGV[0]=~/http:\/\/([^\:\/]+):(\d+)/i){ $host=$1; $port=$2; }elsif ($ARGV[0]=~/http:\/\/([^\:\/]+)/i){ $host=$1; } if ($ARGV[0]=~/http:\/\/[^\/]+(\/.+)/i){ $path=$1; } socket(SOCKET,PF_INET,SOCK_STREAM,6)|| die "ERROR: can not create a socket $!\n"; my $sockaddr = pack_sockaddr_in($port, inet_aton($host)) || die ("ERROR: pack_sockaddr_in: $!"); connect( SOCKET, $sockaddr)|| die "ERROR: can not connect to port $port and host $host\n"; send(SOCKET, "POST $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: webclient-post.pl/1.0\r\nAccept: */*\r\n",0); my $postlen=length($ARGV[1]); send(SOCKET, "Content-Type: application/x-www-form-urlencoded\r\nContent-Length: $postlen\r\n",0); send(SOCKET, "\r\n" , 0); send(SOCKET, "$ARGV[1]" , 0); alarm 5; # kill me after amount of sec my $line; my $inbody=0; my $cl=0; my $bodylen=0; my $start_couting_bodylen=0; while ($line = ) { print "$line"; if ($start_couting_bodylen){ $bodylen+=length($line); if ($bodylen>=$cl){ last; } } if($inbody && $line eq "\r\n"){ last; } if (!$cl && $line =~/^Content-Length: (\d+)/){ $cl=$1; alarm 0; # cancel alarm } if(!$inbody && $line eq "\r\n"){ if ($cl && !$start_couting_bodylen){ print "start_couting_bodylen=1\n"; $start_couting_bodylen=1; } $inbody=1; } } close SOCKET or die "close: $!"; # end of webclient-post.pl __END__