我正在尝试使用Perl的Mechanize库填写Web表单。
每当我在搜索框中输入URL时,它会提示以下消息,我可以手动登录成功。
但是,当我运行以下脚本时,我收到以下错误:
如何使用我的$mech
对象正确填写第一个身份验证盒凭据?
my $mech = WWW::Mechanize->new( 'keepalive' => 1 );
my $url = "http://URL/I/NEED/TO/ACCESS";
my $username = "username";
my $password = "password";
$mech->credentials($username, $password);
$mech->get($url); <----------------- ERROR (LINE 93)
修改
以下是在请求的网站上运行wget
的结果
--2013-08-30 11:16:17-- http://moss.micron.com/MFG/ProbeTest/Lists/Manufacturing%20Requests/AllItems.aspx
Resolving moss.micron.com... 137.201.88.118
Connecting to moss.micron.com|137.201.88.118|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 401 Unauthorized
Server: Microsoft-IIS/7.0
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 12.0.0.6341
Date: Fri, 30 Aug 2013 17:16:17 GMT
Connection: keep-alive
Content-Length: 0
Authorization failed.
答案 0 :(得分:0)
对于旧版本的Mechanize,您可以继承WWW::Mechanize包并提供自己的凭据例程:
package MyMech;
use vars qw(@ISA);
@ISA = qw(WWW::Mechanize);
sub get_basic_credentials {
my ($self, $realm, $uri) = @_;
return( "user", "password" );
}
然后在你的程序中使用这个包而不是WWW :: Mechanize:
package main;
my $mech = MyMech->new();
$mech->get( $url );
<强>更新强>
您已更新问题以指明NTLM身份验证的要求。在CPAN上查看LWP::Authen::Ntlm。