Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

LWP::UserAgent vs WWW::Mechanize

by jcdento (Novice)
on Jun 13, 2008 at 15:22 UTC ( [id://691923]=perlquestion: print w/replies, xml ) Need Help??

jcdento has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am having some trouble running a secure login script. I need mechanize for its link following capabilities, yet i cannot get mech to login using the identical commands as the version of the script using LWP::UserAgent. I am using:
$response = $agent->get($url); my $form = HTML::Form->parse($response->{_content}, $response->base()) +; $form->param("username", "---------"); $form->param("password", "---------"); $response = $agent->request($form->click);
then when i print response content, when $agent is of LWP::Agent i am logged in, but with WWW::Mechanize I am redirected to the login screen. This is EXTREMELY frustrating since this isnt even supposed to be what I am doing for the main part of this project. any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: LWP::UserAgent vs WWW::Mechanize
by pc88mxer (Vicar) on Jun 13, 2008 at 15:53 UTC
    when $agent is of LWP::Agent i am logged in, but with WWW::Mechanize I am redirected to the login screen.
    Do you mean LWP::UserAgent?

    It just so happens that WWW:Mechanize is a sub-class of LWP::UserAgent. So if you can't figure out what's going wrong, an option is to use LWP::UserAgent methods to log in and WWW::Mechanize methods for everything else.

      yes, sorry i meant LWP::UserAgent. The thing is, i am using identical methods in both scripts, and lwp is working, and mech isnt. My objects are:
      y $agent = LWP::UserAgent->new(cookie_jar => HTTP::Cookies->new, reque +sts_redirectable => [ 'GET', 'HEAD', 'POST' ]); $agent->agent( 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8 +.1.14) Gecko/20080404 Firefox/2.0.0.14'); $agent->default_header('Accept' => "text/xml,application/xml,applicati +on/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", 'Accept-Language' => "en-us,en;q=0.5", 'Accept-Charset' => "ISO-8859-1,utf-8;q=0.7,*;q=0.7", 'Keep-Alive' => "300", 'Connection' => "keep-alive");
      and
      my $agent = WWW::Mechanize->new(autocheck=>1, agent=> 'Mozilla/5.0 (Wi +ndows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/ +2.0.0.14'); $agent->max_redirect(100); $agent->default_header('Accept' => "text/xml,application/xml,applicati +on/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", 'Accept-Language' => "en-us,en;q=0.5", 'Accept-Charset' => "ISO-8859-1,utf-8;q=0.7,*;q=0.7", 'Accept-Encoding' => "gzip,deflate", 'Keep-Alive' => "300", 'Connection' => "keep-alive", 'TE' => "");
        WWW::Mechanize's version of ->request modifies the request before issuing it, and that could be the problem. Try this:
        $agent = WWW::Mechanize->new(...); ...set up $agent... $agent->LWP::UserAgent::request($form->click);
Re: LWP::UserAgent vs WWW::Mechanize
by Anonymous Monk on Jun 14, 2008 at 08:16 UTC
    If you're going to use WWW::Mechanize, write
    $agent->get($url); if( $agent->success ){ $agent->form_number(1); $agent->form_with_fields(qw' username password '); $agent->field("username", "---------"); $agent->field("password", "---------"); $agent->submit; if( $agent->success ){ die "I logged in, now I die"; } }
    remember, WWW::Mechanize is Handy web browsing in a Perl object :D

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://691923]
Approved by pc88mxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found