2

I'm trying to get an LWP request working to an https server. I have been given a user & pass, advised to use basic authentication. I've tried various chunks of code, and all seem to get an authentication error. My current code is...

use warnings;
use strict;
use Data::Dumper;
use LWP;

my $ua = LWP::UserAgent->new( keep_alive => 1 );
##also tried by $ua->credentials('domain','','user','pass');
##not sure if I need 'realm' or how I get it, as no popup on screen.
my $request = HTTP::Request->new( GET => "https://my.url.com/somepath/" );
$request->authorization_basic('myuser','mypass');
$request->header( 'Cache-Control' => 'no-cache' );

print $response->content;
print Dumper $response;

The server gives a security error, but if I look at a dump of $response, I see the following...

'_rc' => '401',
'_headers' => bless( {   .... lots of stuff
        'title' => 'Security Exception',
        'client-warning' => 'Missing Authenticate header',
        'client-ssl-socket-class' => 'IO::Socket::SSL',
         ...
        'expires' => '-1'
                                  }, 'HTTP::Headers' ),

'_msg' => 'Unauthorized',
'_request' => bless( {
     '_content' => '',
     '_uri' => bless( do{\(my $o = 'https:theurlabove')}, 'URI::https' ),
     '_method' => 'GET',
     '_uri_canonical' => $VAR1->{'_request'}{'_uri'}
     '_headers' => bless( {
                           'user-agent' => 'libwww-perl/6.04',
                           'cache-control' => 'no-cache',
                           'authorization' => 'Basic dzx..........'
                         }, 'HTTP::Headers' ),

I'm trying to understand whats happening, it looks like in the original request, it has the headers in there, but in the response, its saying I'm 'Missing Authenticate Header'.

Is there something amiss with the code, or something I'm misunderstanding with the request/respinse ?

Thanks.

4

2 回答 2

3

“Missing Authenticate header”消息来自 LWP 本身。这意味着它在目标响应中找不到身份验证标头。如果您有类似的情况,这可能意味着您的代理设置配置错误。

于 2013-09-19T14:18:36.847 回答
1

我不知道这是否是您正在寻找的,但我在尝试对网页进行身份验证时遇到了同样的问题,并且不得不使用 WWW::Mechanize 来解决它。我必须转到第一页并登录,然后请求我想要的页面。

use WWW::Mechanize;


my $loginPage = "http://my.url.com/login.htm";          # Authentication page
my $mech = WWW::Mechanize->new();                       # Create new brower object
$mech->get($loginPage);                                 # Go to login page
$mech->form_name('LogonForm');                          # Search form named LogonForm
$mech->field("username", myuser);                       # Fill out username field
$mech->field("password", mypass);                       # Fill out password field
$mech->click("loginloginbutton");                       # submit form
$mech->get("http://my.url.com/somepath/");              # Get webpage
# Some more code here with $mech->content()
于 2013-09-19T15:15:21.523 回答