简体   繁体   中英

How do I maintain cookies across many WWW::Mechanize runs?

use WWW::Mechanize;
use strict;
my $agent = WWW::Mechanize->new(cookie_jar => {ignore_discard => 0});
$agent->add_header('User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0');
$agent->get($url);
my $content = $agent->content;

The cookie_jar attribute expects a HTTP::Cookies object.

WWW::Mechanize->new(
    cookie_jar => HTTP::Cookies->new(
        file => 'lwp_cookies.dat',
        autosave => 1,
    )
)

Your mistake was to declare a plain hashref, this means a temporary in-memory cookie store that is destroyed after Mechanize ends.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM