HTTP::Cookies inquiry

S. Morgan Friedman (morgan@westegg.com)
Tue, 17 Aug 1999 15:18:20 -0400 (EDT)


	hi everyone,

	i've been working with libwww (and on this list)
	for quite a while, with lots of success. however, 
	these last few days have been my first endeavors 
	with cookies, and i've run into a problem and i 
	can't see what i'm doing wrong, and the 
	documentation has not been able to solve this 
	one for me (as it usually does:-).

	what i'm trying to do is fill out a form on
	one web page. however, in order to access that
	page, they need a cookie placed which they
	send out on an earlier page. so, my little
	program does the following: it goes to the
	front page in which the cookie is set. then
	it follows a link from that page to the page 
	with the form (the form's actually within a
	frame on that page). there are no problems
	on these pages -- the second page, with the
	form, which needs the cookie placed from
	the first page to load, loads beautifully.

	however... i then try to fill out the form
	on this page. whenever i do this, their
	server returns a blank page. from my attempts
	to place a cookie on the earlier pages, i
	discovered that their server responds to
	requests when their is no cookie by just
	returning a blank page -- hence i know that
	this is a cookie problem. i have no idea
	what i'm doing wrong, here is the code,
	it is very straightforward.

	please let me know if you have any clues.
	i'm clueless. thanks! :)

	steven morgan friedman

# ====================================================
#!/usr/bin/perl

use LWP::UserAgent;
use URI::URL;
use HTTP::Request;
use HTTP::Response;
use HTTP::Cookies;

### global cookie_jar variable
$cookie_jar = HTTP::Cookies->new;

$base = "http://www.ypeu.net/";

&main_prog;

sub main_prog {
	local($ua, $req, $res, $form_info);

	### we need to go to these first two pages to get/set the cookies
	### note the form itself is on the second page
	&get_cookie_page($base . "lang_test.asp?language=02&sito=eypmenu.asp");
	&get_cookie_page($base . "cat_ric.asp");

	### fill out the form
	$form_info = "myvar=eypmenu" .
		"&state=Italy" .
		"&cat=Trattorias" .
		"&indcat=" .
		"&reg=Abruzzo" .
		"&indreg=" .
		"&prov=Teramo" .
		"&indcit=" .
		"&nom=Trattoria+Tina";
	$ua = new LWP::UserAgent;
	$ua->cookie_jar($cookie_jar);			# cookie
	my $req = new HTTP::Request 'POST',$base . "test.asp";
	$req->content_type('application/x-www-form-urlencoded');
	$req->content($form_info);
	my $res = $ua->request($req);
	$cookie_jar->extract_cookies($res);		# cookie

	print $res->content;
}

sub get_cookie_page {
	local($doc) = @_;
	local($ua, $req, $res);

	$ua = new LWP::UserAgent;
	$ua->cookie_jar($cookie_jar);			# cookie
	my $req = new HTTP::Request 'GET',$doc;
	$req->header('Accept' => 'text/html');
	my $res = $ua->request($req);
	$cookie_jar->extract_cookies($res);		# cookie

	$res->content;
}