简体   繁体   English

Perl LWP:通过HTTP标头发送Cookie不起作用

[英]Perl LWP: Send Cookie by HTTP header not work

I have known that if you want to use cookie, you must write code as: 我知道,如果您想使用Cookie,则必须将代码编写为:

$ua = LWP::UserAgent->new();
$ua->cookie_jar(HTTP::Cookies->new());

then you can get(), post() and so on. 那么您可以get(),post()等。

But if I just put the cookie string into the HTTP HEADER and get(), I can not get the correct response. 但是,如果仅将cookie字符串放入HTTP HEADER和get()中,则无法获得正确的响应。 As: 如:

$ua = LWP::UserAgent->new();
$ua->get($url, 'Cookie' => $cookie_string);

I think you will find that the Cookie header is being set correctly using that method. 我认为您会发现使用该方法可以正确设置Cookie标头。

To make sure, you can write 为了确保您可以写

my $resp = $ua->get('http://www.myurl.com', Cookie => 'my=data');

print $resp->request->as_string;

and you should see that the request contains the line 并且您应该看到该请求包含以下行

Cookie: my=data

From the docs, it looks like $ua->cookie_jar() expects a hash, either to specify options or to provide a data structure to store the cookies in. I would try 从文档看来, $ua->cookie_jar()期望使用哈希值,以指定选项或提供数据结构来存储cookie。我会尝试

$ua->cookie_jar({});

Also, be aware that the method you are trying to use will only store cookies in memory, so once your script ends, so does your knowledge of said cookies. 另外,请注意,您尝试使用的方法只会将cookie存储在内存中,因此,一旦脚本结束,您对这些cookie的了解也会随之增加。 You may want to look at 您可能想看看

http://metacpan.org/pod/HTTP::Cookies http://metacpan.org/pod/HTTP::Cookies

http://www.perl.com/pub/2002/08/20/perlandlwp.html http://www.perl.com/pub/2002/08/20/perlandlwp.html

For examples of both in-memory cookie jars, and file-based ones. 有关内存cookie罐和基于文件的cookie罐的示例。

As to why HTTP::Cookie wouldn't return a suitable hash when called they way you did, I'm not sure but it appears that it isn't. 至于为什么HTTP :: Cookie以您的方式调用时不会返回合适的哈希值,我不确定,但事实并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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