Pregunta

I have known that if you want to use cookie, you must write code as:

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

then you can get(), post() and so on.

But if I just put the cookie string into the HTTP HEADER and get(), I can not get the correct response. As:

$ua = LWP::UserAgent->new();
$ua->get($url, 'Cookie' => $cookie_string);
¿Fue útil?

Solución

I think you will find that the Cookie header is being set correctly using that method.

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

Otros consejos

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({});

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. You may want to look at

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

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

For examples of both in-memory cookie jars, and file-based ones.

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top