Loading ...
Sorry, an error occurred while loading the content.

Re: HTTP:COOKIE problem

Expand Messages
  • b_harnish
    ... working? ... Turn on warnings! It would have notified you of at least one of the errors below. Also, use strict, its good habit. #!/usr/bin/perl -w use
    Message 1 of 3 , Apr 26, 2002
    • 0 Attachment
      --- In perl-beginner@y..., "Saumitra Sarkar" <saumitras@s...>
      wrote:
      > hi ,
      >
      > Can any one help me out ....why the following script is not
      working?
      > and what value I will have to write at %rest.........
      > --------------------------------
      > #!/usr/bin/perl

      Turn on warnings! It would have notified you of at least one of
      the errors below. Also, use strict, its good habit.
      #!/usr/bin/perl -w
      use strict;

      > print "content-type:text/html\n\n";

      After this line executes, if you want to set a cookie later, you
      can't. Cookies need to be set in the HTTP header. The HTTP header
      ends at the first "\n\n".

      > use LWP::UserAgent;
      > use HTTP::Cookies;
      >
      > my $ua = new LWP::UserAgent;
      > $version='1';
      > $key='xyz';
      > $val ='abc'
      > $path='/';
      > $domain='.mydomain.com';
      > $port='80';
      > $path_spec='1';
      > $secure='0';
      > $maxage='500';
      > $discard='0';
      > %rest='(test=>test)';

      If you had warnings on, it would have said, 'Odd number of
      elements in hash assignment', talking about the %rest line. If
      you take the quotes off of the outside, and put them on the
      second test, it should function the way you want.

      Also, if you 'use strict;', you will have to declare each of
      these variables with 'my'.

      > my $cookie_jar = HTTP::Cookies->new;
      > $ua->agent('Mozilla/8.0' . $ua->agent);
      >
      >
      > print"cookie=$cookie";

      If you're trying to set a cookie in the HTTP Header, it
      should be 'print "Set-Cookie: $cookie\n";' (IIRC).

      > $url='http://www.mydomain.com/abc/xyz.pl';
      > my $req = new HTTP::Request(GET => $url);

      HTTP::Request takes two params, method, and url. You are passing
      a hash instead of a list. Replace the => with a comma ','.

      > $cookie_jar->add_cookie_header($req);
      > my $res = $ua->request($req);
      > if ($res->is_success) {
      > $content=$res->content;
      > $cookie_jar->extract_cookies($res);
      > $cookie_jar->set_cookie($version, $key, $val, $path,
      $domain, $port,
      > $path_spec, $secure, $maxage, $discard,%rest);
      > } else {
      > print "sorry boss\n";
      > }
      > print $content;

      - Brian
    Your message has been successfully submitted and would be delivered to recipients shortly.