Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

HTTP::Cookies with LWP::UserAgent

by dkode (Acolyte)
on May 31, 2001 at 09:19 UTC ( #84462=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks, I'm in a bit of a dilemma. It's a rather stupid question but frankly it's got me stumped.

The Problem:
I am using LWP::UserAgent to connect to a webpage and get a response. I also added in a cookiejar like so:
$ua->cookie_jar(HTTP::Cookies->new(file => "cookies.txt", autosave => +1));
The website has no problem seeting the cookie within cookies.txt. But then the response that the website sends back to me is that it could not set the cookie. Could the website be trying to set the cookie and then check it in the same request? Is this possible? I'm really stumped as I have just started to use HTTP::Cookies and LWP::UserAgent and am sorta confused as to why this i happening. Thank you ahead of time. later DKode

Replies are listed 'Best First'.
Re: HTTP::Cookies with LWP::UserAgent
by Vynce (Friar) on May 31, 2001 at 13:25 UTC

    yes, i think some websites do set and read the same cookie on one request.

    I think the idea is to set and then immediately test a cookie as a method of checking to see if the user accepts cookies. then the server can make theoretically intelligent decisions about what else to do based on this data (e.g., us ealternate methods of keeping track of the user if cookies aren't working)

      Although this has nothing to do I don't get it how it should be possible with the HTTP-Protokoll? There are 2 parts in a HTTP-Request: The request from the client to the server, and the response from the server back to the client. Given the fact that the server sends the cookie in his response how should the client somehow know "before" receiving if the server wants to know about a cookie?

      Only posibility for this I can see: An <img src="/cgi-bin/cookiecheck.pl"> script in that page. Personally I don't think that this is really functional for clients might turn image-loading off, and/or they don't send the cookie back because they just have received it and the user still might have a "Do you want to store this cookie?" window above it's client while the client finishes loading the page in the background. Or is this broken behavior and I'm just thinking queer?

      Anyway, my point still is: It can't be done on a single request, there must at least be two of them. One to send the cookie and the next one that can check if the cookie is set.
      --
      use signature; signature(" So long\nAlfie");

(jeffa) Re: HTTP::Cookies with LWP::UserAgent
by jeffa (Bishop) on May 31, 2001 at 20:45 UTC
    There are two more methods you can try, the first is to use Netscape cookies (if you use Netscape):
    my $file = "$ENV{HOME}/.netscape/cookies"; $ua->cookie_jar( HTTP::Cookies::Netscape->new( file => $file, autosave => 1, ) );
    or you could skip writing the cookie to a file. You didn't mention that you _need_ to save the cookie for later use. If you really only need to cookie around for the life of the script, then just use RAM:
    $ua->cookie_jar(new HTTP::Cookies);

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
      Is there a way to save the cookie for later use? Or is that not possible when using LWP::UserAgent? Whenever it sends a request does it tell the server where to locate the cookie at? Thanks in advance.
        Yes, you can save the cookie to a file by specifying a file name when you create the cookie object. If you only need the cookie around for the session, then you don't need to save it, unless you don't want to re-create it next time you run your bot.

        When a request is sent to the server, the cookie is neatly tucked away in the header of the request. That's how the server gets the cookie - through the browser (or the web bot in our case), which creates the header for the request.

        Jeff

        R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
        L-L--L-L--L-L--L-L--L-L--L-L--L-L--
        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2022-05-28 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (98 votes). Check out past polls.

    Notices?