开发者

How can I send cookies with Perl's LWP::Simple?

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 conf开发者_如何学编程igure that with the get funciton? or do I have to customize my own method?

My main interest is Cookies! then Post!


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜