Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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('');
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);
    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 will set some cookies for you, and not, for example, an image embedded inside html returned by or javascript embedded inside or css embedded inside or one or more combinations of both

    Also, could be 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 interfering

    Also, it could be some firewall or antivirus or somesuch interfering on your machine (or on proxy between you and )

    Also, it could be anything :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1042736]
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2018-01-22 05:25 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (231 votes). Check out past polls.