develooper Front page | perl.libwww | Postings from February 2001

cookie_jar problem

From:
Kai Krebber
Date:
February 16, 2001 04:41
Subject:
cookie_jar problem
Message ID:
200102161241.MAA09156@ireserver.Ireland.Sun.COM
Howdy!

I almost don't dare to ask, but doesn't anybody have trouble getting cookies working for LWP::UserAgent at all?
I used a little script (Listing 19.5 from 'Perl 5 by Example'; e.g.http://www.codebits.com/p5be/ch19.cfm) to test a browsers capability 
of handling cookies and it works fine with Netscape.
However, if I try another little perl - LWP (5.50) script with cookie_jar as client, the testing script complains about the browser not 
beeing able to handle cookies.

Did I miss something?

Kai

---------------------8<-----------------

#!/usr/bin/perl -w

use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request;

my $ua  = LWP::UserAgent->new();
my $jar = HTTP::Cookies->new();

$ua->cookie_jar($jar);
my $req = HTTP::Request->new(GET => "http://localhost/cgi-bin/cookietest.pl");
    $response = $ua->request($req);

    {  # Handle redirects
       if($response->code == 302) {
           $request = HTTP::Request->new(GET =>
                   $response->header('Location'));
           $response = $ua->request($request);
           redo;
       }
    }

if ($response->is_success) {
    print $response->headers_as_string(), "\n",
          $response->content(), "\n\n";
} else {
    print "Error! Code=", $response->code, "\n";
    print "Message=", $response->message, "\n\n";
} 

---------------------8<-----------------

#!/usr/bin/perl
      use strict;
      if ($ENV{'QUERY_STRING'} ne 'TESTING') {
          print "Status: 302 Moved Temporarily\n"; 
          print "Set-Cookie: Cookie=Test\n";
          print "Location: $ENV{'SCRIPT_NAME'}?TESTING\n\n";
      }
      else {
          if ($ENV{'HTTP_COOKIE'} =~ /Cookie=Test/) {
              print("Content-type: text/html\n\n");
              print("<HTML>");
              print("<HEAD><TITLE>$ENV{'HTTP_USER_AGENT'} supports Cookies</TITLE></HEAD>");
              print("<BODY>");
              print("Your browser, $ENV{'HTTP_USER_AGENT'}, supports the Netscape HTTP ");
              print("Cookie Specification.");
              print("</BODY></HTML>");
          }
          else {
              print("Content-type: text/html\n\n");
              print("<HTML>");
              print("<HEAD><TITLE>$ENV{'HTTP_USER_AGENT'} doesn't support Cookies</TITLE></HEAD>");
              print("<BODY>");
              print("Your browser, $ENV{'HTTP_USER_AGENT'}, doesn't appear to support cookies.");
              print("</BODY></HTML>");
          }
      }




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About