Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

LWP::UserAgent doesn't see cookies in header

by zeno (Friar)
on Mar 14, 2002 at 15:48 UTC ( [id://151709]=perlquestion: print w/replies, xml ) Need Help??

zeno has asked for the wisdom of the Perl Monks concerning the following question:

My program needs to enter a web page, get a session number (which is stored in a cookie), and then go to a subsequent page on the same site using that cookie to maintain the session.

I thought this would be simple, using HTTP::Cookies to create a cookie jar. After beating my head against it for a while, I realized that the site was not passing me any set-cookies in the header. But when I visit the page from a browser, and I tell the browser to prompt me if a cookie arrives, the browser tells me the site is sending me cookies.

I even telneted to the site on port 80, and got the same header information with no set-cookies

I distilled the code down to the following. I check two sites, the one which doesn't send me cookies in the header (infojobs) and one which does (msn).

Has anyone seen this before? How are they sending me cookies if they aren't in the header?

# get_header.pl - print out headers of various URLs use strict; use LWP::UserAgent; my @urls; # Create array of URLs to visit push(@urls,"http://www.infojobs.net/empresa_login.cfm"); push(@urls,"http://www.msn.com"); # Create user agent which emulates Internet Explorer my $ua = LWP::UserAgent->new; $ua->agent('Mozilla/4.0 (Compatable; MSIE 5.01; Windows NT 5.0)'); # Cycle through URLs and print out their headers for my $url (@urls) { my $request; my $result; # Create a request object for this URL $request = HTTP::Request->new('GET', $url); $request->header('Accept' => 'text/html'); # Submit the request $result = $ua->request($request); # Print the resulting header print "_" x 80 ."\n"; print "Looking up $url...\n"; print $result->headers_as_string; }

Replies are listed 'Best First'.
Re: LWP::UserAgent doesn't see cookies in header
by derby (Abbot) on Mar 14, 2002 at 16:24 UTC
    zeno,

    Have you tried setting up the cookie_jar for your UserAgent?

    $ua->cookie_jar({ file => "$ENV{HOME}/.cookies.txt" });

    for the infojobs page, the first visit does not send a cookie at all (and I wouldn't expect it to). By not setting up a cookie_jar, LWP::UserAgent will not forward any cookies received from earlier requests.

    -derby

      Yep, but it didn't work out-- I changed the section that defines the user agent to this:

      # Create user agent which emulates Internet Explorer my $ua = LWP::UserAgent->new; $ua->agent('Mozilla/4.0 (Compatable; MSIE 5.01; Windows NT 5.0)'); $ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt", autosave => 1, ignore_discard => 1));

      The msn.com cookies saved to the file just fine (I added the ignore_discard option so it would save session cookies as well). But the InfoJobs site still doesn't show any cookies in the header, and they don't show up in the cookie jar, either.

      Thanks for your response, derby

        zeno,

        Okay, let me get this straight:

        • using browser, when you go to http://www.infojobs.net/empresa_login.cfm, you're browser shows cookies being sent back
        • using LWP, when you go to http://www.infojobs.net/empresa_login.cfm, no cookies are being sent back

        If that's correct, I believe you must all ready have cookies for infojobs.net in your browser's cookie jar and it's doing something different based because when you come to the page you're sending those cookies. When I go to http://www.infojobs.net/empresa_login.cfm with my browser (konqueror), I am not presented with any cookies at all. If you can, copy your browsers cookie to your lwpcookies.txt (being careful of format and what not).

        -derby

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://151709]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-20 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found