问题
I'm trying to fill out a web form using the Mechanize library of Perl.
Whenever I enter the URL into the search box it prompts the below message, and I can manually login sucessfully.
However, when I run the below script, I get the following error:
How do I correctly fill in the first Authentication Box credentials using my $mech
object?
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)
EDIT:
Here are the results of running wget
on the requested site
--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.
回答1:
With older versions of Mechanize you could subclass the WWW::Mechanize package and provide your own credentials routine:
package MyMech;
use vars qw(@ISA);
@ISA = qw(WWW::Mechanize);
sub get_basic_credentials {
my ($self, $realm, $uri) = @_;
return( "user", "password" );
}
Then in your program use this package instead of WWW::Mechanize:
package main;
my $mech = MyMech->new();
$mech->get( $url );
Update
You've updated your question to indicate the requirement of NTLM authentication. Check out LWP::Authen::Ntlm on CPAN.
来源:https://stackoverflow.com/questions/17889817/perl-wwwmechanize-authentication-error-geting-url