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

Hi there, I have a problem, i need to automate a login to a site, but this site needs cookies enabled, i have the following code (code below), yet the response is cookies need to be enabled, any ideas what i am doing wrong???


#!/local/bin/perl -w require LWP; use LWP::Simple; use LWP::UserAgent; use HTTP::Cookies; my $browser = LWP::UserAgent->new(); $cookie_jar = HTTP::Cookies->new('file' => 'C:/Documents and Settings/ +user/Cookies/user@kingsofchaos[2].txt', # where to read/write cookies 'autosave' => 1, # save it to disk when done ); $browser->cookie_jar( $cookie_jar ); $xml = "usrname=data&uemail=data&peeword=data"; $url = "http://www.somesite.com/login.php?$xml"; $reply_msg = $browser->get($url); while (($key,$value) = each(%$reply_msg)) { print "$key - $value\n"; while(($nkey,$nvalue) = each(%$value)) { print "$key - $nkey - $nvalue\n"; # while(($nnkey,$nnvalue) = each(%$nvalue)) # { # print "$key - $nkey - $nnkey - $nnvalue\n"; # } } } exit;

edit: holli added code tags

Replies are listed 'Best First'.
Re: Perl Cookies
by inman (Curate) on Sep 08, 2005 at 11:35 UTC
    You are using HTTP::Cookies which is a generic container for cookies. If you want to read cookies that already exist on your system then you need to use a specialised container.

    It looks like you are using IE. Try HTTP::Cookies::Microsoft

    I find the IE treatment of cookies to be more difficult to work with than the Netscape compatible method supported by Firefox, Mozilla etc. I also find that Mozilla offers better cookie management features so I use it for development (while using Firefox for general browsing).

    You would use HTTP::Cookies::Netscape to work with Netscape compatible files.