cookie_jar problem
Kai Krebber (Kai.Krebber@ireland.sun.com)
Fri, 16 Feb 2001 12:40:17 +0000 (GMT)
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>");
}
}