"Sateless" HTTP and Data Retrival by Posting to Multi-Forms using LWP

jianzhong ding (jding@grx4.bio.bnl.gov)
Wed, 27 May 1998 01:52:01 -0400 (EDT)


Hello Everyone:

	I posted to the bulletin board on the problem of posting to
a form and getting results two days ago and got no response yet. I hope
this time I can get better luck.

	The "stateless" nature of HTTP protocol pushes server side CGI to
use either Cookie (as by Netscape) or some kind of session ID to
faciliate the transactions such as shopping cart, retrival of user
password and username, etc. However this presents a problem of retriving
data using LWP from client side.

	Here is the problem troubled me for past two weeks. To retrive
the data from my school server, first I need input my password and ID in
browser (this is the first form one need to deal with); then I was poped
up a multi-menu page which I can choose one option such as my course
history by pushing the button. As one can see, this is the form that I need
to post and get my real data.  

	How can I retrive my data using LWP? My solution is appended
below. By establishing one HTTP connection, I input my password and ID
to the first form. Without surprise, I do get the main
menu-page. I then started my second request by only modifying the URL
address. What I tried to achieve is to let second request have same
session with the first request, so that the second cgi program will find
the correct password and ID. I failed and never got the results I hope. 

	I am using LWP 5.20 and SSLeay-0.6.6.(to deal with SSL). 

	Can anyone shed light on this? Thanks a lot for your help.


Jianzhong Ding


****************************************************************
Code ahead
****************************************************************

use LWP::Debug qw(+);
use URI::URL;
use LWP::UserAgent;
#use HTTP::Cookies;

$ua = new LWP::UserAgent;
$ua->agent("Mozilla/2.0 (compatible; MSIE 3.02; Update a; AK; Windows 95)");

######### first URL which ask for password and ID
$url1 = 'https://adam.cc.sunysb.edu/cgi-bin/cgi-cwis/cwismain.cgi';

###### second URL which ask for nothing except by pushgin some button such as course history
$url2 = 'https://adam.cc.sunysb.edu/cgi-bin/cgi-cwis/option.cgi';

%form1 = ( cID=>'xxxxxxx',
	  cPIN=>'xxxxxx',	
	  db=>'prod' );		# hidden type

%form2 = ( PushButton=>' USB Course History ',  
          db=>prod,		# hidden type
          cID=>'xxxxxxx',	# hidden type
          cumb=>'C',		# hidden type
          cyr=>'1998',		# hidden type
          f1umb=>'A',		# hidden type
          f1yr=>'1998',		# hidden type
          f2umb=>'0',		# hidden type
          f2yr=>'0000',		# hidden type
          ACSYr=>'1998' );	# hidden type

my $req1 = new HTTP::Request 'POST', $url1; 
$req1->content_type("application/x-www-form-urlencoded");

my $curl1 = url("https:");      # create an empty HTTP URL object
$curl1->query_form(%form1);
$req1->content($curl1->equery); # %form content as escaped query string

my $res1 = $ua->request($req1);

# Check the outcome of the res4ponse
if ($res1->is_success ) {
	print $res1->content;
	}
	else {
	print "Bad luck this time\n";
	}

#####################################################################
#	this starts the second post, url2 is the address of the second form
#
#####################################################################

$req1->url($url2);	# just modify the URL address to post to the second form

my $curl2 = url("https:");      # create an empty HTTP URL object
$curl2->query_form(%form2);

$req1->content($curl2->equery); # %form content as escaped query string

my $res2 = $ua->request($req1);

# Check the outcome of the res4ponse
if ($res2->is_success ) {
        print $res2->content;
        }
        else {
        print "Bad luck this time\n";
        }