perl获取会话cookie
不适合做饼干perl获取会话cookie,perl,session,cookies,Perl,Session,Cookies,不适合做饼干 #!/usr/bin/perl -w use strict; use warnings; use LWP::UserAgent; use HTTP::Request::Common qw(GET); use HTTP::Cookies; my $ua = LWP::UserAgent->new; # Define user agent type $ua->agent('Mozilla/4.0'); # Cookies $ua->cookie_jar(
#!/usr/bin/perl -w
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common qw(GET);
use HTTP::Cookies;
my $ua = LWP::UserAgent->new;
# Define user agent type
$ua->agent('Mozilla/4.0');
# Cookies
$ua->cookie_jar(
HTTP::Cookies->new(
file => 'mycookies.txt',
autosave => 1
)
);
# Request object
my $req = GET 'http://www.google.com';
# Make the request
my $res = $ua->request($req);
# Check the response
if ($res->is_success) {
print $res->content;
} else {
print $res->status_line . "\n";
}
exit 0;
当cookie是这样的时候(来自firebug)
mycokies.txt是
#LWP-Cookies-1.0
Set-Cookie3:
PREF="ID=00349dffbc142a77:FF=0:TM=1311217451:LM=1311217451:S=QKw9G4vAwl19Me4g";
path="/"; domain=.google.com; path_spec; expires="2013-07-20 03:04:11Z"; version=0
但对于某些站点,当cookie看起来像这样时
name value
verify test
guest_id 131099303870438180
PHPSESSID 7s99iq1qcamooidrop4iehcv32
mycokies.txt中没有任何内容
如何修复它
谢谢。您的第一个cookie是一个域cookie,将在将来过期。所以它会被写到饼干罐里
第二个cookie是会话cookie,在程序关闭时过期。它被保存在内存中,不会写入jar。我意识到这有点晚了。选中的答案仅描述了出现此问题的原因。要真正“修复”这个问题,您需要查看HTTP::Cookies的ignore_discard参数 这也解决了我的问题,在
HTTP::Cookies->new
参数中添加ignore\u discard=>1
name value
verify test
guest_id 131099303870438180
PHPSESSID 7s99iq1qcamooidrop4iehcv32