我试图运行这个 perl5 程序:
#!/usr/bin/env perl
use strict;
use warnings;
use LWP;
my $ua = LWP::UserAgent->new('Mozilla');
$ua->credentials("test.server.com:39272", "realm-name", 'user_name', 'some_pass');
my $res = $ua->get('http://test.server.com:39272/');
print $res->content;
另一方面,我有 HTTP::Daemon:
#!/usr/bin/env perl
use strict;
use warnings;
use HTTP::Daemon;
my $hd = HTTP::Daemon->new or die;
print "Contact URL: ", $hd->url, "\n";
while (my $hc = $hd->accept) {
while (my $hr = $hc->get_request) {
if ($hr->method eq 'GET') {
print $hr->as_string, "\n";
}
}
$hc->close;
undef($hc);
}
它只是打印:
Contact URL: http://test.server.com:39272/
GET / HTTP/1.1
Connection: TE, close
Host: test.server.com:39272
TE: deflate,gzip;q=0.3
User-Agent: libwww-perl/6.03
所以我看到 LWP::UserAgent 不发送 HTTP 基本身份验证,但我不知道为什么。
我在这个网站上看到了一些帖子,但他们有相同的基本代码,但它不起作用......
如果我使用 HTTP::Request 那么它可以工作:
my $req = GET 'http://test.server.com:39272/';
$req->authorization_basic('my_id', 'my_pass');
my $res = $ua->request($req);
输出:
GET / HTTP/1.1
Connection: TE, close
Authorization: Basic bXlfaWQ6bXlfcGFzcw==
Host: test.server.com:39272
TE: deflate,gzip;q=0.3
User-Agent: libwww-perl/6.03
我之前是不是做错了什么?