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);

In reply to LWP w/ Cookie Based Logins by lhoward

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.