Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I have been trying to GET a page which requires a cookie to be received properly. What I eventually want to do is to POST some info to a form on this page and do a login with the help of LWP.

I know there are some other nodes on this subject, like this one LWP, extract_cookies, etc. , but I can't figure out what is wrong in my case.. The cookie which I receive does not seem to be "picked up" and is not saved to file (I save cookies in file for debugging purpose of this script).

What I bascily want to do is just to GET the url where the form, that I'm going to POST to later, is located. If I can receive the url with correct cookie handling, I'm sure POST'ing will not be difficult either. Here is my code:

#!c:/perl/bin/perl use strict; use LWP::UserAgent; use HTTP::Request; use HTTP::Cookies; use HTTP::Response; use HTTP::Headers; use HTTP::Message; use HTTP::Request::Common; use LWP::Debug; use CGI; #STORE COOKIES IN FILE my $cookie_file = qq(c:/tmp3/mycookie.txt); #HOW MANY TIMES HAVE $cookie_jar BEEN CHANGED local $main::called_cookie = 0; my $query = new CGI; print $query->header; #NEW USER-AGENT OBJEKT my $ua = LWP::UserAgent->new; $ua->agent('Mozilla/4.0'); #NEW COOKIE_JAR my $cookie_jar=HTTP::Cookies->new ('file' => "$cookie_file", 'autosave' => '1'); #SUB TO BE CALLED WHEN $cookie_jar CHANGES _ FOR DEBUGGING $cookie_jar->scan(\&cookiesub); $cookie_jar->clear(); #VARIOUS TEST-SITES WHERE WE RECEIVE COOKIES #my $req = GET ''; #my $req = GET '' ; #my $req = GET ''; #WANTED !! my $req = GET ''; #GO GET INFO my $resp=$ua->request($req); #UPDATE $cookie_jar; $cookie_jar->extract_cookies($resp); #PRINT RETURNED HEADERS print $resp->headers_as_string(); #SHOW VALUE OF $cookie_jar NOW print "\n\nMy Cookie_jar is now:".$cookie_jar->as_string."\n\n"; #SHOW RESPONSE #print "\n\n".$resp->as_string; #SAVE $cookie_jar TO FILE; $cookie_jar->save(); exit; sub cookiesub { print "\n\n----------------\n\Cookie has been altered"; print "\nCookie_counter:$main::called_cookie \n"; $main::called_cookie++; print "@_"; print "\n----------------\n"; }
I receive reply that I need to "accept cookies" when I GET the wanted url. No cookie-values are saved to file, but when I print the cookie value with
print "\n\nMy Cookie_jar is now:".$cookie_jar->as_string."\n\n";
I get:
Set-Cookie3: NETSCAPE_LIVEWIRE_ID=305A68095915248EF4A4BD81DABFD4F90021 +A1F2; path="/Login"; domain=""; path_spec; discard; v +ersion=0
What is wrong? Should this not have been handled automatic by LWP::UserAgent? What Can be done to receive the wanted URL with correct cookie handling?
I've tried doing several GET's after updating $cookie_jar with
but that does not seem to help.

Also when I try to GET the amazon url, I see 3 Set-Cookies Headers in the HTTP::Response object ($res), but only 2 is saved to file? What is that all about?

I've been banging my head in the wall for too long about this. I need some help!

I'd REALLY appreciate any tips.
Regards, Tom

In reply to LWP cookie mystery by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (3)
    As of 2020-07-14 16:03 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found