This section provides a tutorial example on how to use Perl LWP package to simulate a Web browser and debug ASP pages at the HTTP communication level.
If you have a problem with your ASP application at the HTTP communication level,
one good debugging tool is the Perl LWP package. It can be used as a Web browser
to talk to your ASP application, and to log everything at the HTTP communication level.
Here is my sample Perl program, reg_client.pl, designed to work with my previous
ASP registration application:
#- reg_client.pl
#- Copyright (c) 2002 by Dr. Herong Yang
use LWP::Debug qw(+);
use LWP::UserAgent;
use HTTP::Cookies;
($url) = @ARGV;
$url = 'http://localhost' unless $url;
$ua = new LWP::UserAgent;
$cookie_jar = HTTP::Cookies->new;
&getForm();
&submitForm();
exit;
sub getForm {
$u = $url.'/reg_form.asp';
my $req = new HTTP::Request GET => $u;
my $res = $ua->request($req);
$req = $res->request();
$cookie_jar->extract_cookies($res);
&dump($req,$res);
}
sub submitForm {
$u = $url.'/reg_form.asp?first_name=Herong&submit=Submit';
my $req = new HTTP::Request GET => $u;
$cookie_jar->add_cookie_header($req);
my $res = $ua->request($req);
$req = $res->request();
$cookie_jar->extract_cookies($res);
&dump($req,$res);
}
sub dump {
local ($req,$res) = @_;
print "\nREQUEST-HEADERS\n";
print $req->headers_as_string();
print "\nREQUEST-CONTENT\n";
print $req->content;
if ($res->is_success) {
print "\nRESPONSE-HEADERS\n";
print $res->headers_as_string();
print "\nRESPONSE-CONTENT\n";
print $res->content;
} else {
print "\nRESPONSE-ERROR\n";
print $res->error_as_HTML();
}
}
A couple of notes to help you to understand this program:
"use LWP::Debug qw(+);" turns on the debugging at the highest level.
A "LWP::UserAgent" object is used to send a HTTP request to the HTTP server.
"HTTP:Request" objects are used to compose HTTP requests.
"$cookie_jar->extract_cookies($res);" is used to extract cookies from the
response. This is very important, because ASP server is sending the session ID
as a cookie to the client and expecting the client to send it back in the next
request.
"$cookie_jar->add_cookie_header($req);" is used to add the cookies received
from the previous response to the current request. One of the cookies is the
session id, which is important for the ASP server to recognize the current request
is a continuation of the previous request.