http://www.perlmonks.org?node_id=246146

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks
Basically i have created a socket of the type
#!/usr/bin/perl use IO::Socket; $sock = new IO::Socket::INET ( PeerAddr => 'www.yahoo.com', PeerPort => 80, Proto => 'tcp', Timeout => 10, ); die "Socket could not be created $!\n" unless $sock; print $sock "GET /page?pg=somewhere.html HTTP /1.0\nUser-Agent: Mozil +la/4.0 (compatible; MSIE 5.01; Windows 98; ocnie5-1)\nContent-Type: t +ext/html; charset=EUC-JP\n\n"; while($line = <$sock>) { print "$line\n"; } exit 0;
I have used yahoo just for ex sake. When i give the
User-Agent header and check the log files, i am not getting it
. The user agent is getting printed as it is
2 I-1373-067 User-Agent: Mozilla/4.0 (compatible; MSI +E 5.01; Windows 98; ocnie5-1)
I suppose there is some thing wrong with the format of User-Agent.
Can anyone help me
Thanks in advance

Replies are listed 'Best First'.
Re: User-Agent
by pg (Canon) on Mar 27, 2003 at 03:08 UTC
    Just remove one blank, your code now works. In between 'HTTP' and '/1.0', there should be no space, i.e. it should be 'HTTP/1.0'.

    use IO::Socket; $sock = new IO::Socket::INET ( PeerAddr => 'www.yahoo.com', PeerPort => 80, Proto => 'tcp', Timeout => 10, ); die "Socket could not be created $!\n" unless $sock; print $sock "GET /page?pg=somewhere.html HTTP/1.0\nUser-Agent: Mozill +a/4.0 (compatible; MSIE 5.01; Windows 98; ocnie5-1)\nContent-Type: te +xt/html; charset=EUC-JP\n\n"; while($line = <$sock>) { print "$line\n"; } exit 0;
    Now yahoo no longer complains about received a bad request.

    Of course, because the page you requested does not exist on yahoo, yahoo would reply and say that the page you requested didn't exist, but your request is now well formated.

Re: User-Agent
by blokhead (Monsignor) on Mar 27, 2003 at 02:39 UTC
    1. use LWP; You won't have to learn the intricacies of HTTP conversations.
    2. I fail to see what's wrong with the user agent string you are getting in the log files. It's exactly the same string you passed to the HTTP server. Or am I missing something? Is that example line not from your log files?

    blokhead

      It is a cut and paste from the Log files.
      As i said even i am not able to understand what is wrong.
      It is an old program which i have to change and hence cant use LWP modules.
      Thanks

        If you're changing it, why not make it better?

        Use LWP. It will make things much more easier for you, as well as pave the way for greater functionality, if you desire.

        John J Reiser
        newrisedesigns.com