Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Problems? Is your data what you think it is?
 
PerlMonks  

lwp+w/o jsessionid

by opensourcer (Monk)
on Sep 20, 2008 at 18:53 UTC ( #712762=perlquestion: print w/ replies, xml ) Need Help??
opensourcer has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

how do i disable jsession id or change the headers using lwp. I'm writing a scraper for ISP login page and i found out the header something like this using Live HTTP headers plugin of firefox
https://logindel.tataindicombroadband.in:8443/user/refresh/home?user=x +xxxxxxx&ServerList=vsnl.net&save=2&imageField.y=21&mageField.x=31&t=f +lcwmm4m',
and lwp sends something like the following
'location' => 'https://logindel.tataindicombroadband.in:8443/user/refr +esh/home;jsessionid=z6toaqyuja0p?user=xxxxxx&ServerList=vsnl.net&t=fl +cx2pnr',

I want to remove the jsession.
my code
$uri = ""https://logindel.tataindicombroadband.in:8443/user/refresh/ho +me"; use LWP::UserAgent; use HTTP::Cookies; my $ua = LWP::UserAgent->new(keep_alive => 300); $ua->agent("MyBrowser 8.0"); my $cookie_jar = HTTP::Cookies->new( file => $Config::cookie, autosave => 1, ); $ua->cookie_jar($cookie_jar); print "1\n"; my $req = $ua->post($uri, ["user" => $username, "username" => $username, "password" => $password, "ServerList" => "vsnl.net", ]); die unless $req->is_success; }

Comment on lwp+w/o jsessionid
Select or Download Code
Re: lwp+w/o jsessionid
by ikegami (Pope) on Sep 21, 2008 at 01:57 UTC

    One way:

    Create a subclass of LWP::UserAgent that overrides request to remove the jsessionid from the requested url.

    use strict; use warnings; package My::UserAgent; BEGIN { our @ISA = 'LWP::UserAgent'; } use LWP::UserAgent qw( ); use URI qw( ); use URI::QueryParam qw( ); sub request { my $self = shift; my $request = shift; my $uri = URI->new( $request->uri() ); $uri->query_param_delete( 'jsessionid' ); $request->uri( $uri ); return $self->SUPER::request( $request, @_ ); } 1;

    Beware that this might cause infinite loops if the server insists on you using jsessionid. Well, not quite infinite since LWP::UserAgent stops redirecting after max_redirect redirects (default is 7).

    Update: Fixed bug mentioned in reply. Added use strict; and use warnings;.

      throws error "Undefined subroutine &main:: called at UserAgent.pm line 16."
      I named package as UserAgent
        $uri->query_param_delete->( 'jsessionid' );
        should be
        $uri->query_param_delete( 'jsessionid' );

        Tested.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2014-04-21 04:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls