http://www.perlmonks.org?node_id=981285

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

I tried to access the authenticated page config.cgi using the below code. But I am not able to access them with the authenticated session. I get only the login page at the second request. Somebody please tell me whats wrong in this script.
my $ua = LWP::UserAgent->new; $ua->agent("MyApp/0.1 "); $url = 'http://localhost/web_authenticate_post.cgi'; $req = HTTP::Request->new(POST => $url); $req->content_type('application/x-www-form-urlencoded'); $req->content('username=security&password=security&login=Login'); my $res=$ua->request($req); if($res->is_success){ $res1 = $ua->request(HTTP::Request->new(GET=>'http://localhost/conf +ig.cgi' )); print $res1->content; } else{ print $res->status_line; }

Replies are listed 'Best First'.
Re: Need to Access one page after authentication in perl
by Anonymous Monk on Jul 12, 2012 at 06:44 UTC
Re: Need to Access one page after authentication in perl
by monsoon (Pilgrim) on Jul 12, 2012 at 02:57 UTC
    A shot in the dark as i'm not really familiar with this stuff. I thought the ampersands in the url encoded string need to be replaced with percent escape "%26". Or are the web servers forgiving about that?
Re: Need to Access one page after authentication in perl
by bulk88 (Priest) on Jul 12, 2012 at 04:58 UTC
    You need to use cookies. How is the server supposed to know you logged in otherwise? Basically set a "cookie jar", and any cookies received will wind up automatically in the cookie db. The db/cookie jar is usually not saved to the disk in most LWP programs and just stays around for the runtime life of the process.