5

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?

Soma Holiday
  • 185
  • 1
  • 1
  • 13
  • 2
    possible duplicate of [Why don't my LWP::UserAgent credentials work?](http://stackoverflow.com/questions/1799147/why-dont-my-lwpuseragent-credentials-work) – Anil Jan 03 '14 at 00:49
  • That question is similar, and one that I read prior to posting this one. However, I don't have a solution, and I'd appreciate if someone walked me through using Charles or some other tool to solve the issue. – Soma Holiday Jan 03 '14 at 12:41

2 Answers2

1

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.

hobbs
  • 223,387
  • 19
  • 210
  • 288
0

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.

Anil
  • 2,539
  • 6
  • 33
  • 42