Soma Holiday
Soma Holiday

Reputation: 185

HTTPS Request with Perl UserAgent

I am trying to request an html document with HTTPS authentication using Perl. I haven't had an issue with non-HTTPS code in the past. I'm currently using LWP::UserAgent:

#! /usr/bin/perl
use LWP;
use HTTP::Request::Common;

my $browser = LWP::UserAgent->new;
my $baseurl = "https://xyz.url.com/directory";

my $ua = LWP::UserAgent->new(keep_alive=>1);
$ua->cookie_jar({});
$ua->credentials('xyz.url.com:80', '', 'login', 'password');

my $response = $ua->get($baseurl);
print  $response->content();

The response that is printed is essentially the '401 Unauthorized' page. I am able to login via the browser. The browser uses a pop-up that reads 'Authentication Required -- The server xyz.url.com:80 requires a username and password. The server says: Blah Blah' I have read many posts instructing to install things like Crypt::SSLeay and LWP::Protocol::https. These are installed. I'm stumped.

I've also downloaded a program called Charles to see if there is anything of note with the request/response, however I'm not really sure where to look. I've tried adding 'Blah Blah' for the realm, but this wasn't successful. Can the realm be identified using Charles?

Upvotes: 5

Views: 11757

Answers (2)

hobbs
hobbs

Reputation: 240010

The documentation says that the first argument to credentials is <host>:<port> — that is, the port number isn't optional. Try "xyz.url.com:443". Also, a realm of '' isn't going to be valid unless that's what the server is sending; you actually want to use "Blah Blah" for the realm, I believe.

Upvotes: 1

Anil
Anil

Reputation: 2554

Perhaps it's because you have not set a proper agent. Try this:

#! /usr/bin/perl
use LWP;
use HTTP::Request::Common;

my $baseurl = "https://xyz.url.com/directory";

my $ua = LWP::UserAgent->new(keep_alive=>1);
$ua->agent('Mozilla/5.0');
$ua->cookie_jar({});
$ua->credentials('xyz.url.com', '', 'login', 'password');

my $response = $ua->get($baseurl);
print  $response->content();

Also, I removed the variable $browser in my code as it appears you were not using it for anything.

Upvotes: 0

Related Questions