Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

LWP w/ Cookie Based Logins

by lhoward (Vicar)
on Jul 03, 2000 at 17:33 UTC ( #20877=snippet: print w/ replies, xml ) Need Help??

Description: Many people have recenely been asking about how to use LWP to access a site that they have to login to; where the login is cookie based. I present the sample code below to serve as an example of a technique for handling these situations. Though the code itself doesn't do anythig really useful (it adds lhoward to a perlmonks user personal nodelet) it can serve as an example for anyone else working on a similar problem.
#!/usr/bin/perl -w

use strict;
use LWP::UserAgent;
use URI::URL;
use HTTP::Cookies;


# put your perlmonks username and password here
my $pm_user='';
my $pm_password='';

#instantiate new user agent
my $WWWAgent = new LWP::UserAgent();


# attach cookiejar to user agent
my $co=new HTTP::Cookies(file=>'./cookies.dat',autosave=>1);
$WWWAgent->cookie_jar($co);


# build request to simulate login
my $url=new URI::URL 'http://www.perlmonks.org/index.pl';
$url->query_form(
        op      =>      "login",
        node_id =>      "109",
        lastnode_id =>  "131",
        user    =>      $pm_user,
        passwd  =>      $pm_password);
my $WWWRequest = new HTTP::Request 'GET', $url->as_string() ;
# submit request
my $WWWResult = $WWWAgent->request($WWWRequest);
die "Error logging in $WWWResult->code $WWWResult->message" 
        if(!$WWWResult->is_success);


# build request to add "lhoward" to personal nodelet
$url=new URI::URL 'http://www.perlmonks.org/index.pl';
$url->query_form(
        addpersonalnodelet      =>      5549,
        node_id                 =>      109,
        lastnode_id             =>      109);
$WWWRequest = new HTTP::Request 'GET', $url->as_string() ;
# submit request
$WWWResult = $WWWAgent->request($WWWRequest);
die "Error adding to nodelet $WWWResult->code $WWWResult->message"
        if(!$WWWResult->is_success);
Comment on LWP w/ Cookie Based Logins
Download Code

Back to Snippets Section

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-12-19 12:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (81 votes), past polls