in reply to Fetching HTML Pages with Sockets

You code won't work with many servers as they require a Host: header.

sub socket_get { my ( $url, $cookies ) = @_; return ("HTTP/1.0 500 Invalid URL $url", '' ) unless $url =~ m!^http://([^/:\@]+)(?::(\d+))?(/\S*)?$!; my $host = $1; my $port = $2 || 80; my $path = $3; $path = "/" unless defined $path; require IO::Socket::INET; my $sock = IO::Socket::INET->new( PeerAddr => $host, Proto => 'tcp', PeerPort => $port, ) or return ( "HTTP/1.0 500 Could not connect socket: $url", '' +); $sock->autoflush; my $netloc = $host; $netloc .= ":$port" if $port != 80; my $cookie_str = ''; if ( $cookies and ref($cookies) eq 'ARRAY' ) { $cookie_str .= "Cookie: $_\015\012" for @$cookies; } my $req = join '', "GET $path HTTP/1.0\015\012", "Host: $netloc\015\012", "Accept: */*\015\012", "Accept-Encoding: *\015\012", $cookie_str, "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Window +s 98; )\015\012\015\012"; syswrite( $sock, $req ); my ($header, $content, $buffer) = ('','',''); $content .= $buffer while sysread( $sock, $buffer, 8192 ); $sock->close; return ( "HTTP/1.0 500 Failed to get any content for $url", '' ) unl +ess $content; ( $header, $content ) = split /\015\012\015\012|\012\012|\015\015/ +, $content, 2; return ( "HTTP/1.0 500 Failed to get a header for $url", '' ) unless + $header; # unfold the header $header =~ s/\015\012/\n/g; $header =~ s/\n\s+/ /g; $content ||= ''; return ( $header, $content ); }

cheers

tachyon