4
use LWP::Simple;
use HTML::LinkExtor;
use Data::Dumper;
#my $url = shift @ARGV;
my $content = get('example.com?GET=whateverIwant');
my $parser = HTML::LinkExtor->new(); #create LinkExtor object with no callbacks
$parser->parse($content); #parse content

now if I want to send POST and COOKIE info as well with the HTTP header how can I configure that with the get funciton? or do I have to customize my own method?

My main interest is Cookies! then Post!

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Neo
  • 11,078
  • 2
  • 68
  • 79

2 Answers2

4

LWP::Simple is for very simple HTTP GET requests. If you need to do anything more complex (like cookies), you have to upgrade to a full LWP::UserAgent. The cookie_jar is a HTTP::Cookies object, and you can use its set_cookie method to add a cookie.

use LWP::UserAgent;

my $ua = LWP::UserAgent->new(cookie_jar => {}); # create an empty cookie jar

$ua->cookie_jar->set_cookie(...);

my $rsp = $ua->get('example.com?GET=whateverIwant');
die $rsp->status_line unless $rsp->is_success;
my $content = $rsp->decoded_content;
...

The LWP::UserAgent also has a post method.

cjm
  • 61,471
  • 9
  • 126
  • 175
  • 2
    But what are the arguments to set_cookie! The documentation refers to $version, but doesn't give any details! http://search.cpan.org/~gaas/HTTP-Cookies-6.01/lib/HTTP/Cookies.pm#METHODS – Chloe Jan 14 '13 at 19:25
  • @Chloe, it expects you to be familiar with the cookie spec. See [RFC 2965](https://www.ietf.org/rfc/rfc2965). – cjm Jan 14 '13 at 23:56
2

You might want to use WWW::Mechanize instead. It already glues together most of the stuff that you want:

 use WWW::Mechanize;
 
 my $mech = WWW::Mechanize->new;

 $mech->cookie_jar->set_cookie(...);

 $mech->get( ... );

 my @links = $mech->links;
brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • You don't need `cookie_jar => {}` here, because WWW::Mechanize already defaults to that (unlike its base class, LWP::UserAgent, which does not create a cookie_jar unless requested to). – cjm Oct 13 '10 at 07:45
  • Heh, I can never remember which way it goes. :) – brian d foy Oct 13 '10 at 15:47