简体   繁体   English

Perl和LWP无法验证

[英]Perl and LWP not authenticating

I'm trying to get an LWP request working to an https server. 我正在尝试让LWP请求在https服务器上工作。 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... 服务器给出了安全错误,但是如果我查看$ response的转储,则会看到以下内容...

'_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. 谢谢。

The "Missing Authenticate header" message is coming from LWP itself. “ Missing Authenticate标头”消息来自LWP本身。 This means that it couldn't find an authenticate header in the target response. 这意味着它在目标响应中找不到身份验证标头。 This might mean that your proxy settings are misconfigured, if you have anything like that. 如果您有类似的情况,这可能意味着您的代理设置配置错误。

I don't know if this is what you are looking for but I came across the same problem trying to authenticate to a webpage and had to solve it with WWW::Mechanize. 我不知道这是否是您要寻找的东西,但是我遇到了同样的问题,试图对网页进行身份验证,不得不使用WWW :: Mechanize解决它。 I had to go to the first page and login then request the page I wanted. 我必须转到第一页并登录,然后请求我想要的页面。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM