Perl 获取会话 cookie
对某些 cookie 不起作用
#!/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)时,
name value
PREF ID=00349dffbc142a77:FF=0:LD=en:CR=2:TM=1311217451:LM=1311217451:S=QKw9G4vAwl19Me4g
mycookies.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
mycookies.txt 中没有任何内容,
如何修复它。
谢谢。
is not work for some cookie
#!/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;
when cookie is like this ( from firebug )
name value
PREF ID=00349dffbc142a77:FF=0:LD=en:CR=2:TM=1311217451:LM=1311217451:S=QKw9G4vAwl19Me4g
mycookies.txt is
#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
but for some site when cookie look like this
name value
verify test
guest_id 131099303870438180
PHPSESSID 7s99iq1qcamooidrop4iehcv32
nothing in mycookies.txt
how to fix it.
thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的第一个 cookie 是域 cookie,将来会过期。所以它被写入 cookie 罐中。
第二个 cookie 是会话 cookie,当程序关闭时就会过期。它保存在内存中,并且不会写入 jar 中。
Your first cookie is a domain cookie with expiry in the future. So it gets written to the cookie jar.
The second cookie is a session cookie, and expires when the program closes. It gets kept in memory, and does not get written to the jar.
我意识到这有点晚了。检查的答案仅描述了您遇到此问题的原因。要真正“修复”问题,您需要查看 HTTP::Cookies 的ignore_discard 参数。
I realize this is a bit late. The checked answer only describes why you have this problem. To actually "fix" the problem you need to look into the ignore_discard parameter for HTTP::Cookies.