Hi all,
I'm just learning Perl and have been thrown into the fire so to speak. I have many
years of programming background but because of my limited experience with Perl and the
nature of the project I'm working on (wanting results quickly) I'm having some
questions that are probably pretty obvious to seasoned vets.
I've bastardized someone's code and so far things are working pretty well. In the code
I open a specific URL using HTML and LWP modules, find a specific form on the page,
enter a username and password and use ->click to submit the form. The
$click->is_redirect() function is returning true which is what I want.
But at this point I actually want to grab the redirected page in the same way I had
grabbed the original page and scan the new page for specific forms that need to be
filled in. The $click variable appears to have the status of the HTTP form submit. How
do I get the information off of the redirected page at this point? I get the original
information using an HTTP::Request. But in this case I'm using the ->click to return a
page of information.
I apologize if this is making no sense. Since I am using someone else's code I still
have a lot of fuzziness about what is actually happening. A little nudge in the right
direction would be helpful and greatly appreciated.
So let me reiterate to make it as clear as possible.
1) I pass a URL, username, password
2) The URL is opened and scanned for a login form
3) The form once found is populated with the passed username and password
4) form->click is called which makes the $click->is_redirect() routine equal to true
at this point
5) open the redirected page as in #2 above and do some more form and submit stuff
(this is where I'm having problems... I don't know how to 'open' this redirected page)
Thanks so much,
Guy Davis
[EMAIL PROTECTED]
The code being used follows:
sub main {
my $loginurl = "https://secure.overture.com/s/dtc/center/"; # LOGIN URL
my ($post_status, $rcode) = post_message($loginurl, $USER_NAME,
$USER_PASSWORD, $SEARCH_TERM, $BID_AMOUNT);
### HTTP_error lets you define which HTTP error codes will stop the process.
if ( HTTP_error($rcode) ) {
# return error code and status
print "error ".$post_status."\n";
} else {
# return aok
print "Everything OK!\n";
}
return $rcode;
}
sub post_message {
my $uri = shift;
my $u_name = shift;
my $u_pass = shift;
my $u_term = shift;
my $u_bid = shift;
my $ua = shift || new LWP::UserAgent;
my $agent = shift || 'Mozilla/4.0 (compatible; MSIE 5.0; Windows NT;
DigExt)';
## Set some User Agent properties.
$ua->agent($agent);
$ua->timeout(30);
## Convert the URL into a url object (with a couple methods we can use).
my $u = new URI::URL( $uri );
## Fetch the web page.
my $req = HTTP::Request->new(GET=>$u->as_string);
my $res = $ua->request($req);
my @forms;
my $guestbook_html = '';
if ($res->is_success) {
@forms = HTML::Form->parse($res->content, $u->abs);
}
else {
return ('COULD_NOT_FETCH', $res->code);
}
## Get which form object will be used to login.
my $form_num = login_form( @forms ); # Real numbers, starting at 1;
return ('NO_FORMS', $res->code) unless $form_num;
my $form = $forms[$form_num - 1];
my $action = $forms[$form_num - 1]->action;
## Keeps track of the fields we have found.
my %is_field = (
realname => 0,
url => 0,
comments => 0,
);
my $fresult;
## Set form values...
($form->value( 'UserName', $u_name), $is_field{UserName} = 1) if
$form->find_input( 'UserName', 'text' );
($form->value( '/go2/directraffic/handler/LoginFormHandler.password',
$u_pass), $is_field{password} = 1) if $form->find_input(
'/go2/directraffic/handler/LoginFormHandler.password', 'password' );
## If we found the PRIMARY required fields, then let's try to post.
if ($is_field{UserName} && $is_field{password}) {
my $click = $ua->request( $form->click );
if ( $click->is_success() || $click->is_redirect() ) {
### We should have been redirected to another page
my $p = HTML::LinkExtor->new(undef, $u->abs());
$p->parse( $click->as_string );
print "p=$p\n";
return ('SUCCESS', $click->code());
}
return ('UNSUCCESSFUL_POST', $click->code());
}
return ('UNSUCCESSFUL_POST', '000');
}
sub login_form {
my @forms = @_;
## Returns the which form object will be used to login. Shift index to 1
for my $i (0..$#forms) {
return ($i+1) if (index($forms[$i]->action, 'index.jhtml') != -1);
}
return 0;
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]