in reply to Re^2: Problems with IO::Socket::INET
in thread Problems with IO::Socket::INET

I just thought I would post a final follow-up to this thread. Thanks to the advice of dave_the_m, I have this working splendidly and figure a snippet of code might help someone who wants a lightweight and simpla alternative to LWP for whatever reason (mine is that Smoothwall doesn't have it and I don't want to distribute it). Here is code that will work ($host, $port, $filename, $username, $password are all just plain strings passed in from somewhere else):
use IO::Socket::INET; use MIME::Base64 (); $pass64 = MIME::Base64::encode_base64("$username:$password", ""); my $sock; unless ($sock = new IO::Socket::INET (PeerAddr => $host, PeerPort => $ +port, Proto => 'tcp', Timeout => 5)) { $errormessage = $tr{'could not connect to http://$host:$port/$file +name'}; return '0'; } my $request = ""; my $eol = "\015\012"; $request = $request."GET /$filename HTTP/1.0".$eol; $request = $request."User-Agent: Perl/5.8.0".$eol; $request = $request."Host: $host:$port".$eol; $request = $request."Accept: */*".$eol; $request = $request."Connection: Keep-Alive".$eol; if ($auth_required) { $request = $request."Authorization: Basic $pass64".$eol; } $request = $request."$eol"; $sock->print($request); while (<$sock>) { printf $_; } close($sock);

Obviously, in that while loop, you will want to do something more useful with the data than print it to the console :) I put each line into an array element.