Perl 获取会话 cookie

发布于 2024-11-25 11:41:58 字数 1272 浏览 0 评论 0原文

对某些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁与争疯 2024-12-02 11:41:58

您的第一个 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.

冷情妓 2024-12-02 11:41:58

我意识到这有点晚了。检查的答案仅描述了您遇到此问题的原因。要真正“修复”问题,您需要查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文