EvanK has asked for the wisdom of the Perl Monks concerning the following question:
Note: for the purpose of these tests, I'm requesting to "http://localhost/mech-form.php" which is just a page with a form that submits to itself and prints out its current session id.
Storable dies complaining that it can't store CODE items, so I'm using YAML's freeze/thaw and DumpFile/LoadFile instead:use strict; use WWW::Mechanize; use Storable qw(store retrieve freeze thaw); # init a mech instance my $mech = WWW::Mechanize->new(); # on first instance... if(! -f 'stored_mech.dat') { # create, get, freeze, and store $mech->get('http://localhost/mech-form.php'); store([freeze($mech)], 'stored_mech.dat'); exit; } # on subsequent instance... else { # retrieve, thaw, and submit form my $stored = retrieve('stored_mech.dat'); $mech = thaw($stored->[0]); $mech->submit_form(form_number=>1); }
But after I load the mech object and try to submit the form, I get the following error:use strict; use WWW::Mechanize; use YAML qw(DumpFile LoadFile freeze thaw); # init a mech instance my $mech = WWW::Mechanize->new(); # on first instance... if(! -f 'stored_mech.dat') { # get, freeze, and store $mech->get('http://localhost/mech-form.php'); DumpFile('stored_mech.dat', [freeze($mech)]); exit; } # on subsequent instance... else { # retrieve, thaw, and submit form my $stored = LoadFile('stored_mech.dat'); $mech = thaw($stored->[0]); $mech->submit_form(form_number=>1); }
I decided to try and save/load the cookies as well. That way, after I load the mech object, I can re-get the page, then just reload the saved cookie jar which should continue the same sessionCan't locate object method "scheme" via package "URI::http" at /usr/sh +are/perl5/URI.pm line 52.
However, it seems like it's still using a new session after i reload the cookie jar, which I confirmed by dumping the response headers and page content (which contains the server-side session id):use strict; use WWW::Mechanize; use YAML qw(DumpFile LoadFile freeze thaw); # init a mech instance my $mech = WWW::Mechanize->new(cookie_jar => {ignore_discard => 0}); # + save even browser-lifetime cookies # on first instance... if(! -f 'stored_mech.dat') { # get, freeze, save cookie jar, and store $mech->get('http://localhost/mech-form.php'); $mech->cookie_jar->save('cookies.dat'); DumpFile('stored_mech.dat', [freeze($mech)]); exit; } # on subsequent instance... else { # retrieve, thaw my $stored = LoadFile('stored_mech.dat'); $mech = thaw($stored->[0]); # re-get page and reload cookies $mech->get('http://localhost/mech-form.php'); $mech->cookie_jar->clear(); $mech->cookie_jar->load('cookies.dat'); # submit form with (presumably) original session $mech->submit_form(form_number=>1); }
This results in the output below (after I've stripped the page's markup and the non-related headers):use strict; use WWW::Mechanize; use YAML qw(DumpFile LoadFile freeze thaw); # init a mech instance my $mech = WWW::Mechanize->new(cookie_jar => {ignore_discard => 0}); # + save even browser-lifetime cookies # on first instance... if(! -f 'stored_mech.dat') { # get, freeze, save cookie jar, and store $mech->get('http://localhost/mech-form.php'); $mech->cookie_jar->save('cookies.dat'); printf "Before freeze & save: \%s\n\n", $mech->response()->headers +()->as_string(); printf "Page session id: \%s\n\n", $mech->content(); DumpFile('stored_mech.dat', [freeze($mech)]); exit; } # on subsequent instance... else { # retrieve, thaw my $stored = LoadFile('stored_mech.dat'); $mech = thaw($stored->[0]); # re-get page and reload cookies $mech->get('http://localhost/mech-form.php'); $mech->cookie_jar->clear(); $mech->cookie_jar->load('cookies.dat'); # submit form with (presumably) original session $mech->submit_form(form_number=>1); printf "After thaw & submit: \%s\n", $mech->response()->headers()- +>as_string(); printf "Page session id: \%s\n\n", $mech->content(); }
So even after clearing and reloading the cookie jar, it's not keeping the same session across multiple instances of this script...and I have no clue where to go from here. Is there something I'm missing? Am I going to have to go mucking about in the Mechanize internals?~$ perl ./mech-test.pl Before freeze & save: Set-Cookie: PHPSESSID=p9ogedv2qf1r5h664la79is1j2 +; path=/ Page session id: p9ogedv2qf1r5h664la79is1j2 ~$ perl ./mech-test.pl After thaw & submit: Set-Cookie: PHPSESSID=tq3devkvch2t5fsq6qbp707de4; + path=/ Page session id: tq3devkvch2t5fsq6qbp707de4
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Maintaining sessions with WWW::Mechanize across script instances?
by Eliya (Vicar) on Apr 21, 2011 at 20:28 UTC | |
by EvanK (Chaplain) on Apr 22, 2011 at 08:52 UTC | |
|
Re: Maintaining sessions with WWW::Mechanize across script instances?
by trwww (Priest) on Apr 22, 2011 at 00:54 UTC |