Thanks Zentara. I have understood it. I am doing something similar. I
need to invoke a SOAP service using HTTP POST. I have gotten to a point
where it invokes the SOAP service but apparently my XML is not well
formed or I am not sending the right info in the header. For example I
am not sending the following (from your code). I will try to use them
and see if it makes a difference.
Thanks
Sumit
"Host: zentara.zentara.net",
"Content-Length: $length",
"",
"Content-Disposition: form-data; name=\"file\"; filename=\"$upfile\"",
zentara wrote:
On Mon, 12 Feb 2007 18:30:00 -0500, [EMAIL PROTECTED] (Sumit Shah)
wrote:
All,
I would appreciate if someone could tell me how to do an HTTP Post using
IO::Socket. I cannot use LWP. I have to manage it with plain Sockets.
I am new to this and any help would be appreciated.
Thanks
Sumit
In the interest of education, I'll post this. Don't ask me to explain
it, if you don't understand it, use the LWP modules.
#!/usr/bin/perl
use warnings;
use strict;
use Socket;
my $url = "http://zentara.zentara.net/~zentara/cgi-bin/up1.cgi";
my $upfile = shift || 'ztest.png';
my $host = "zentara.zentara.net";
$| = 1;
my $start = times;
my ( $iaddr, $paddr, $proto );
$iaddr = inet_aton($host);
#$iaddr = ( gethostbyname($host) )[4];
$paddr = sockaddr_in( 80, $iaddr );
$proto = getprotobyname('tcp');
unless ( socket( SOCK, PF_INET, SOCK_STREAM, $proto ) ) {
die "ERROR : init socket: $!";
}
unless ( connect( SOCK, $paddr ) ) { die "no connect: $!\n"; }
my $length = 0;
open (UH,"< $upfile") or warn "$!\n";
$length += -s UH;
my @head = (
"POST /~zentara/cgi-bin/up1.cgi HTTP/1.1",
"Host: zentara.zentara.net",
"User-Agent: z-uploader",
"Content-Length: $length",
"Content-Type: multipart/form-data; boundary=zzzzzzzzzzzzzzzzzzz",
"",
"--zzzzzzzzzzzzzzzzzzz",
"Content-Disposition: form-data; name=\"file\"; filename=\"$upfile\"",
"Content-Type: application/octet-stream",
"",
"",
);
#try to get total length
my $header = join( "\r\n", @head );
$length += length($header);
$head[3] = "Content-Length: $length"; #2472
$header = join( "\r\n", @head );
$length = -s UH;
$length += length($header);
select SOCK;
$| = 1;
binmode SOCK;
print SOCK $header;
while( sysread(UH, my $buf, 8196 ) ){
if( length($buf) < 8196 ){
$buf = $buf."\r\n--zzzzzzzzzzzzzzzzzzz--";
syswrite SOCK, $buf, length($buf);
}else{ syswrite SOCK, $buf, 8196 }
print STDOUT '.',
}
close UH;
shutdown SOCK, 1;
my @data = (<SOCK>);
print STDOUT "result->@data\n";
close SOCK;
__END__
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/