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

Fellow monks,

I am writing a program that fetches a web page using IO Socket (it can not use LWP) Problem is, the page seems to be behind a standard apache htaccess, thus requiring a username and password to be sent with the http request.

I've looked in the documentation for a way to pass a user and password over http with IO Socket, but can't find any. Is there something I'm missing?

Replies are listed 'Best First'.
Re: user and pass in IO socket
by Kanji (Parson) on Dec 04, 2001 at 15:22 UTC

    You don't say why you "can not use LWP", but if you're going to purposely cripple yourself then you need to at least be reading the correct documentation as (IO::)Socket is only a small part of the picture.

    To fill in the rest, you need to read RFCs 2617, 2616, and perhaps even 1945.

        --k.


      Thanks for the pointer Kanji, I eventually figured out that my request needed a line like:
      Authorization: Basic foobar

      For the posterity of future monks, my final code looks like this:


      snip
      my $userPass = base64("user:password"); # Need two \n in the get request my $get =<<EOF; GET $path HTTP/1.0 Authorization: Basic $userPass EOF # Open the socket my $http_request = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $host, PeerPort => $port, ) or return $!; print $http_request $get; # Write the request my $webPage; { # Grab the output local($/) = undef; $webPage = <$http_request>; } return $webPage;

      snip
Re: user and pass in IO socket
by rob_au (Abbot) on Dec 04, 2001 at 14:19 UTC
    Rather than using IO::Socket and connecting directly to the HTTP port, why don't you try using LWP::UserAgent and HTTP::Request. eg.

    my $ua = LWP::UserAgent->new( keep_alive => 1 ); my $request = new HTTP::Request GET => "http://www.perlmonks.org"; $request->authorization_basic("username", "password"); my $response = $ua->request($request); if ($response->is_success) { print STDOUT $response->content; } else { print STDERR "Error retrieving page!\n"; };

    This combination, not only allows you to authenticate against the web server that you are retrieving the web page from, but you can also make use of proxy network configurations and exert greater control over the request and subsequent response.

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'