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

Hi Monks,

I'd like to know ways to dump an instance of an LWP::UserAgent or WWW::Mechanize browser session to a new browser or tie it to an existing one (cookies, etc)

This is use LWP up to a certain point where to let users browse from

i've tried the following but, although it works, the session does not last ( I assume browser actually has no cookie at all) and i get redirected to login page

#!c:\\perl\\bin\\perl.exe -w use strict; use diagnostics; use WWW::Mechanize; my $mech = WWW::Mechanize->new( autocheck => 1 , agent_alias => "Windows IE 6" , noproxy => 1 ); my $url= shift; # lwp actual navigation # request login page # fill in user and password # submit $mech->get( $url ); $mech->field( 'user_field_name', shift ); $mech->field( 'pwd_field_name' , shift ); $mech->submit; # launch a browser... system('start iexplore.exe '. $mech->uri.";jsessionid=".$mech->cookie_ +jar->{'COOKIES'}->{'server'}->{'/webapp'}->{'JSESSIONID'}->[1]);

Replies are listed 'Best First'.
Re: dump lwp::useragent to browser
by Corion (Patriarch) on Dec 16, 2009 at 15:58 UTC

    Handing off a cookie is not easy. You can look at the various HTTP::Cookies modules for how to inject cookies into the browser's storage, but I've never tried injecting cookies into a live browser session.

    If you're not bent on using WWW::Mechanize, there are alternatives for remote-controlling a web browser. For example, there is Win32::IE::Mechanize to automate Internet Explorer, and there is my module, WWW::Mechanize::Firefox. With these, you can automate the navigation, and you usually get Javascript support thrown in at no extra cost. With WWW::Mechanize::Firefox you can also inject cookies into the running Firefox session, if you want to keep the existing navigation code.

Re: dump lwp::useragent to browser
by afoken (Chancellor) on Dec 16, 2009 at 21:13 UTC

    Problems with your code:

    • Cookies and URL parameters are two really different things. Simply passing a cookie as URL parameter usually does not work, simply because (on a properly coded server) cookies, URL parameters and POSTed parameters are three completely independant name spaces. There are commercial server systems where this is not the case, but they are rare and usually not available from the internet.
    • WWW::Mechanize and your IE will probably send very different HTTP headers, even when you tell W::M to identify as IE 6. A sufficiently paranoid server will detect this and refuse to accept the faked IE session.
    • I see no tests after $mech->submit() that the login really worked.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)