This very simple script will help you learn a bit more about HTTP headers and how they work. It asks for a small amount of information and then creates a simple, but valid HTTP request. The following is a simple HEAD request from Perlmonks:
Request method (GET, TRACE, OPTIONS, or HEAD - HEAD is default)? h Enter host: www.perlmonks.org Port (80 is default)? Path (/index.html is default)? Attempting to connect to www.perlmonks.org:80 HEAD /index.html HTTP/1.1 Host: www.perlmonks.org HTTP/1.1 200 OK Date: Sun, 26 Nov 2000 21:17:19 GMT Server: Apache/1.3.9 (Unix) mod_perl/1.21 Last-Modified: Fri, 17 Nov 2000 02:57:44 GMT ETag: "23e95c-0-3a149ea8" Accept-Ranges: bytes Content-Length: 0 Connection: close Content-Type: text/html X-Pad: avoid browser bug
#!/usr/bin/perl -w use strict; use IO::Socket; my %method = ( g => 'GET', h => 'HEAD', o => 'OPTIONS', t => 'TRACE' ); my $method = ''; while ( $method !~ /^[gtho]/i ) { print 'Request method (GET, TRACE, OPTIONS, or HEAD - HEAD is defa +ult)? '; $method = <STDIN>; $method = 'HEAD' if ! $method; } $method = $method{ lc substr $method, 0, 1 }; my $host = ''; while ( ! $host ) { print 'Enter host: '; chomp ( $host = <STDIN> ); } my $port = 0; while ( $port < 1 or $port > 65535 ) { print 'Port (80 is default)? '; chomp ( $port = <STDIN> ); $port = 80 if ! $port; } my $path = ''; while ( ! $path ) { print 'Path (', $method eq 'OPTIONS' ? '*' : '/index.html', ' is + default)? '; chomp ( $path = <STDIN> ); if ( ! $path ) { $path = ( $method eq 'OPTIONS' ) ? '*' : '/index.html'; } } my $maxForwards = 0; if ( $method eq 'TRACE' ) { while ( $maxForwards < 1 ) { print 'Max forwards? '; chomp ( $maxForwards = <STDIN> ); } } print "Attempting to connect to $host:$port\n"; my $target = new IO::Socket::INET("$host:$port"); my $request = "$method $path HTTP/1.1\n" . "Host: $host\n"; $request .= "Max-Forwards: $maxForwards\n" if $method eq 'TRACE'; $request .= "\n"; print "\n$request"; # Send the headers to the host print $target $request; my $response; { undef $/; # Here are the headers $response = <$target>; } print $response;

In reply to Learning HTTP headers by Ovid

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.