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


In reply to Re: Fetching HTML Pages with Sockets by tachyon
in thread Fetching HTML Pages with Sockets by amt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.