Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

form not being filled out by WWW::Mechanize

by Special_K (Sexton)
on Mar 12, 2013 at 14:52 UTC ( #1022991=perlquestion: print w/ replies, xml ) Need Help??
Special_K has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm trying to use WWW::Mechanize to interact with a website. I'm having trouble getting past the username/password screen, however. Here is what I have:

#!/usr/bin/perl -w use strict; use WWW::Mechanize; my $m = WWW::Mechanize->new(); $m->agent_alias('Linux Mozilla'); $m->get('http://alumni.nd.edu/s/1210/start.aspx?sid=1210&gid=1&pgid=3& +cid=40'); printf("%s\n", $m->title()); $m->dump_forms(); $response = $m->submit_form( form_number => 1, fields => { 'cid_40$txtUsername' => '<username>', 'cid_40$txtPassword' => '<password>', } ); die unless ($m->success); #printf("response message is %s\n", $response->message); printf("%s\n", $m->title());

I receive a response of "OK" from the web server when I run the script, yet the title of the page hasn't changed, indicating to me that the username and password fields aren't being filled out. Given the content returned by $m->dump_forms(), am I filling out the username and password fields correctly? What else should I check for?

Comment on form not being filled out by WWW::Mechanize
Download Code
Re: form not being filled out by WWW::Mechanize
by roboticus (Canon) on Mar 12, 2013 at 15:00 UTC

    Special_K:

    I don't use WWW::Mechanize, but it appears that you're printing the title from your original request. I don't see you looking at the response to get the title.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: form not being filled out by WWW::Mechanize
by sundialsvc4 (Monsignor) on Mar 12, 2013 at 17:18 UTC

    Bear in mind that $m->success only tells you that the response was HTTP 202 OK, not that the content of the returned screen was what you anticipated it should be!   All that this tells you is that the server responded with a new page.   You have to now check the content of the HTTP data-stream that was returned, to determine what to do next.

    The host undoubtedly did send you back some sort of a page ... it’s up to you to figure out whether that page indicates a successful login or not.

      Here is my updated code:

      #!/usr/bin/perl -w use strict; use WWW::Mechanize; my $response = ""; my $m = WWW::Mechanize->new(); $m->agent_alias('Linux Mozilla'); #$m->get('https://msp.f-secure.com/web-test/common/test.html'); $m->get('http://alumni.nd.edu/s/1210/start.aspx?sid=1210&gid=1&pgid=3& +cid=40'); printf("%s\n", $m->title()); $m->dump_forms(); $response = $m->submit_form( form_number => 1, fields => { 'cid_40$txtUsername' => '<username>', 'cid_40$txtPassword' => '<password>', } ); die unless ($m->success); printf("response code is %s\n", $response->code); printf("response message is %s\n", $response->message); printf("response content is %s\n", $response->content); printf("response header time is %s\n", $response->header('date'));

      The above should be correctly parsing the response from the command. The only problem is that the response doesn't seem to indicate what is going on:

      response code is 200 response message is OK response content is (tons of binary data) response header time is Wed, 13 Mar 2013 03:36:18 GMT

      I can't figure out from the above what is going on. Does anyone have any hints/tips? Note that I get the above response even if I enter a deliberately incorrect username and/or password.

        $m->res->dump; ## I AM DEBUGGING HERE $m->content; ## DECODED CONTENT HERE, if gzipped, its gunzipped , etc

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (17)
As of 2014-08-22 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (161 votes), past polls