No such thing as a small change PerlMonks

### Getting Cookies

by PerlSufi (Friar)
 on Jul 05, 2013 at 15:26 UTC ( #1042736=perlquestion: print w/ replies, xml ) Need Help??
PerlSufi has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,
I want to extract cookies from a website. Particularly, JSESSIONID cookie. However, when I run the code below, nothing turns up in my file except '#LWP-Cookies-1.0'.
use LWP::UserAgent;
use HTTP::Cookies;

my $cookie_jar = HTTP::Cookies->new( file => 'C:\path\to\cookies.dat', + autosave=> 1); my$browser = LWP::UserAgent->new( );

$browser->cookie_jar($cookie_jar );

$browser->get('https://example.com'); [download] Any insight into what I'm doing wrong is greatly appreciated! Replies are listed 'Best First'. Re: Getting Cookies by kennethk (Abbot) on Jul 05, 2013 at 16:02 UTC Your code above is correct, assuming that your cookie isn't timing out (i.e. isn't a session cookie). Under normal circumstances, session cookies are discarded immediately upon exit from your 'browser'. If you want to force holding onto the cookie, use ignore_discard in your constructor: my$cookie_jar = HTTP::Cookies->new(file => 'C:\path\to\cookies.dat',
autosave=> 1,
ignore_discard => 1);
[download]
The flag is documented in HTTP::Cookies, and read HTTP_cookie#Session_cookie to learn more about the behavior.

#11929 First ask yourself How would I do this without a computer?' Then have the computer do it the same way.

Thanks, kennethk. That worked for me. :)
The cookie is different from what my browser gives me so I'm working on how to get that now
Firebug, the free Firefox plug-in, makes that easy.

I'd also mention that, instead of storing cookies to disk, you can use Data::Dumper to look at your cookie jar contents.

#11929 First ask yourself How would I do this without a computer?' Then have the computer do it the same way.

Re: Getting Cookies (webbug)
by Anonymous Monk on Jul 06, 2013 at 06:46 UTC

Well, for some reason you think https://example.com/ will set some cookies for you, and not, for example, an image embedded inside html returned by https://example.com/ or javascript embedded inside or css embedded inside or one or more combinations of both

Also, could be https://example.com/ doesn't like the headers you're sending, so it doesn't give you a cookie

Also, it could be a proxy of some sort between you and https://example.com/ interfering

Also, it could be some firewall or antivirus or somesuch interfering on your machine (or on proxy between you and https://example.com/ )

Also, it could be anything :)

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1042736]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2016-07-24 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What is your favorite alternate name for a (specific) keyboard key?

Results (221 votes). Check out past polls.