Basic HTTP Authentication, LWP::UserAgent and the Realm

| No Comments
I recently put up a copy of our main site, www.cindyruppert.com, on a new server for test purposes and pointed www.cindyruppert.net at it.  I didn't want Google (or anyone else) to see all of duplicate content, so I put some basic HTTP Authentication on it, using the .htaccess file:
AuthUserFile /usr/home/myuserid/mypasswords
AuthName     "Protected"
AuthType     Basic
<Limit GET POST PUT>
require valid-user
</Limit>
Then I wanted to access the site using LWP::UserAgent. I googled the terms and came up with lots of interesting results, but still had to poke around too much to get the code to work.

My main problem was the parameter to the LWP::UserAgent credentials method called "realm".  I had no idea what that was.  I finally realized that it was the "AuthName" in the above .htaccess snippet.  Once that piece fell into place, the following code worked:

use strict;
use warnings;
use LWP::UserAgent;

my $site = 'www.cindyruppert.net';

my $ua = LWP::UserAgent->new;

$ua->credentials(
	"$site:80", 
	'Protected', 
	'guest' => 'visitor'
);

my $response = $ua->get("http://$site");
unless ($response->is_success) {
	die "Error\n", $response->status_line, "\n";
}

print "Bytes returned: ", length $response->content, "\n";

exit 0;

Leave a comment

About this Entry

This page contains a single entry by Bill Ruppert published on August 25, 2012 8:01 AM.

Using Perl and GD to generate QR codes was the previous entry in this blog.

Updated Net::Google::Analytics Example is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.38