Hey Monks,

I have been sweating over this issue for 2 days: For some reason when I use credentials in the following way, the debug states the agent is unauthorized for use even after I set up the credentials as per instructions:

#!/usr/bin/perl use warnings; use strict; use LWP::Debug qw( + ); use WWW::Mechanize; use CGI; use MIME::Base64; use HTTP::Cookies; my $netloc = "10.4.4.4:8080"; my $url = "http://" . $netloc . "/admin"; my $realm = "Realm"; my $username = "user"; my $password = "pass"; my $cookie_jar = HTTP::Cookies->new( file => 'cookies.dat', autosave => 1, ); my $agent = WWW::Mechanize->new( autocheck => 1, quiet => 0, agent_alias => 'Mac Mozilla', cookie_jar => $cookie_jar, ); # Supply the necessary credentials $agent->credentials($netloc, $realm, $username, $password); $agent->get($url); # Throw error if page not found $agent->success() or die "Can't fetch the Requested page";
The output is as follows:
LWP::UserAgent::new: () LWP::UserAgent::request: () LWP::UserAgent::send_request: GET http://10.4.4.4:8080/admin LWP::Protocol::http::request: () LWP::UserAgent::request: Simple response: Unauthorized LWP::UserAgent::request: () HTTP::Cookies::add_cookie_header: Checking 10.4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking 4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4.4 for cookies HTTP::Cookies::add_cookie_header: Checking 4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4 for cookies LWP::UserAgent::send_request: GET http://10.4.4.4:8080/admin LWP::Protocol::http::request: () LWP::UserAgent::request: Simple response: Unauthorized LWP::UserAgent::request: () HTTP::Cookies::add_cookie_header: Checking 10.4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking 4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4.4 for cookies HTTP::Cookies::add_cookie_header: Checking 4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4 for cookies LWP::UserAgent::send_request: GET http://10.4.4.4:8080/admin LWP::Protocol::http::request: () LWP::UserAgent::request: Simple response: Unauthorized LWP::UserAgent::request: () HTTP::Cookies::add_cookie_header: Checking 10.4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking 4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4.4 for cookies HTTP::Cookies::add_cookie_header: Checking 4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4 for cookies LWP::UserAgent::send_request: GET http://10.4.4.4:8080/admin LWP::Protocol::http::request: () LWP::UserAgent::request: Simple response: Unauthorized LWP::UserAgent::request: () HTTP::Cookies::add_cookie_header: Checking 10.4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking 4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4.4 for cookies HTTP::Cookies::add_cookie_header: Checking 4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4 for cookies LWP::UserAgent::send_request: GET http://10.4.4.4:8080/admin LWP::Protocol::http::request: () Error GETing http://10.4.4.4:8080/admin: Unauthorized at /Library/Perl +/5.8.8/LWP/Authen/Basic.pm line 56
Then I add in the basic authentication information via MIME::Base64::encode AND tack that info to the $agent->get() call as follows AND IT WORKED!
#!/usr/bin/perl use warnings; use strict; use LWP::Debug qw( + ); use WWW::Mechanize; use CGI; use MIME::Base64; use HTTP::Cookies; my $netloc = "10.4.4.4:8080"; my $url = "http://" . $netloc . "/admin"; my $realm = "Realm"; my $username = "user"; my $password = "pass"; my @args = ( Authorization => "Basic " . MIME::Base64::encode( $username . ':' . $password ) ); my $cookie_jar = HTTP::Cookies->new( file => 'cookies.dat', autosave => 1, ); my $agent = WWW::Mechanize->new( autocheck => 1, quiet => 0, agent_alias => 'Mac Mozilla', cookie_jar => $cookie_jar, ); # Supply the necessary credentials $agent->credentials($netloc, $realm, $username, $password); $agent->get($url,@args); # Throw error if page not found $agent->success() or die "Can't fetch the Requested page";
Output:
LWP::UserAgent::new: () LWP::UserAgent::request: () HTTP::Cookies::add_cookie_header: Checking 10.4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking 4.4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4.4 for cookies HTTP::Cookies::add_cookie_header: Checking 4.4 for cookies HTTP::Cookies::add_cookie_header: Checking .4 for cookies LWP::UserAgent::send_request: GET http://10.4.4.4:8080/admin LWP::Protocol::http::request: () HTTP::Cookies::extract_cookies: Set cookie JSESSIONID => blahblahblah LWP::UserAgent::request: Simple response: OK WWW::Mechanize=HASH(0x9429e0)->status() WWW::Mechanize=HASH(0x9429e0)->content()

Is there something I am doing wrong in the first code example? The web server I am going against is JBOSS v4.2.2.
Thank you for your input. /rob


In reply to Problems with WWW::Mechanize by robmarano

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.