$cookie_jar->add_cookie_header($request);
you use this object correctly (though variable name is very misleading). On this line your program crashes because $cookie_jar->add_cookie_header expects HTTP::Request object instead of HTTP::Response. Next line also is not going to work
because $ua->request also expects HTTP::Request object.
Well, this is the reasons why it crashes. Anyway I'm not sure what are you trying to achive. Seems that you just want to make two POST requests. Why do you want to handle redirects yourself? LWP can do it itself. Why do you want to handle cookies yourself? LWP again can do it itself. Let me show my version of this code.
#!/usr/lib/perl
# be good boy and always do following
use strict;
use warnings;
# you don't have to use other HTTP modules because
# LWP::UserAgent uses them itself
use LWP::UserAgent;
use HTTP::Request::Common;
use HTTP::Cookies;
my $ua = LWP::UserAgent->new;
# this method call creates cookie jar object and tells
# LWP::UserAgent object to use it automatically. You do not
# have to call extract_cookies or add_cookie_headers
# yourself
$ua->cookie_jar(HTTP::Cookies->new(file => 'cookie_jar', autosave =>1)
+;
# configure LWP::UserAgent to follow redirects after POST
push @{ $ua->requests_redirectable }, 'POST';
# first POST request - no need to handle redirects youself.
# Let LWP handle them automatically. This is why I use
# 'request' instead of 'simple_request'.
$ua->request(POST "http://www.sitedomain.com/logi
+n.cfm",
{
username =>'abcuser',
userpass =>'abc123',
submit =>'Submit'
});
# Second POST request - same as in your code
my $request = $ua->request(POST "http://www.sitedomain.com/page.cfm",
{
firstname =>"Michael",
lastname =>"Jensen",
company =>"companyname",
address =>"111 East 222 South",
city =>"Provo",
state =>"UT",
zip =>"99999"
});
print $request->is_success ? "worked\n" : "failed\n";
BTW are you trying to write automated test for login form? Consider using web testing modules like HTTP::WebTest or HTTP::Monkeywrench.
P.S. And of course let's repeat it again: Use strict and warnings!
Update: s/simple_request/request/, added code to turn on handling of redirects after POST in LWP::UserAgent, added use HTTP::Cookies.
Update: added missing ';'
--
Ilya Martynov
(http://martynov.org/)
|