2

https サーバーに対して機能する LWP 要求を取得しようとしています。ユーザーとパスが与えられ、基本認証を使用するようにアドバイスされました。さまざまなコードのチャンクを試しましたが、すべて認証エラーが発生するようです。私の現在のコードは...

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;

サーバーはセキュリティ エラーを返しますが、$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' ),

何が起こっているのかを理解しようとしています。元のリクエストにはヘッダーが含まれているように見えますが、応答には「認証ヘッダーがありません」と表示されます。

コードに何か問題がありますか、それとも request/respinse で誤解していますか?

ありがとう。

4

2 に答える 2

1

これがあなたが探しているものかどうかはわかりませんが、Web ページへの認証を試みているときに同じ問題に遭遇し、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 に答える