mak has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I finally was able to get the code running for login to a site (thanks to some anonymous person's help). The way I do it is that I parse the form, populate login information and send it with cookies. I get the following error when I set the cookie.

Can't locate object method "host" via package "URI::_generic" at D:/Perl/site/lib/URI/WithBase.pm line 41. I printed out debug statements to find out where the problem occurs which is Step 12 (after the statement (print "55\n"'). Can someone please tell me what possibly could go wrong?

use HTTP::Cookies; use HTML::Form; use LWP; use URI; require 'dumpvar.pl'; my $LOGIN = ''; # put your login name here my $PASSWD = '';# password here my $ua = LWP::UserAgent->new(); # Step 1 $ua->agent('Mozilla'); # habit my $url = 'http://www.genesisreports.com/genesis/user/login.asp'; # St +ep 2 my $login_req = HTTP::Request->new(GET => $url); # Step 2 my $login_res = $ua->request($login_req); # Step 3 my $html = $login_res->content; my $form = HTML::Form->parse($html,'http://www.genesisreports.com/gene +sis/user/login.asp'); # Step 4 dumpValue(\$form); # Step 5 $form->value('artist',$LOGIN); # Step 6 $form->value('albumcode',$PASSWD); # Step 6 my $req = $form->click; # Step 7 print "11\n"; my $res = $ua->request($req); # Step 8 print "22\n"; my $cookie_jar = HTTP::Cookies->new(); # Step 9 $cookie_jar->extract_cookies($res); # Step 9 print "33\n"; my $goto = $res->header('location'); # Step 10 print "44\n"; my $new_req = HTTP::Request->new(GET => $goto); # Step 11 print "55\n"; $cookie_jar->add_cookie_header($new_req); # Step 12 print "66\n"; my $new_res = $ua->request($new_req); # Step 13 print "77\n"; # All done or you could keep going. # Parse this page for forms, URL's, or data # and have fun:) print $new_res->headers_as_string,"\n\n"; print $new_res->content,"\n";

Replies are listed 'Best First'.
Re: Form Parsing / Cookie setup Error
by arturo (Vicar) on May 17, 2001 at 20:25 UTC

    The relevant bit (in my estimation):

    my $new_req = HTTP::Request->new(GET => $goto); # Step 11 print "55\n"; $cookie_jar->add_cookie_header($new_req); # Step 12

    First thing I'd want to know is whether that call to HTTP::Request->new() succeeds. A general moral : check the values of system calls. That command fetches a web page, basically, and it could potentially fail. If it does, you can't go on processing. So add or die "Can't get $goto: $!\n"; to the first line. That's one place things might fail. Another is where you actually set $goto ... make sure you have a value to pass to HTTP::Request! You can fix that problem with the same kind of logic (add or die).

    An alternative, for non-fatal errors, is to warn instead of die. In this particular case, I would recommend having the program logic cease processing the link at any point of failure.

    HTH!

    perl -e 'print "How sweet does a rose smell? "; chomp $n = <STDIN>; $r +ose = "smells sweet to degree $n"; *other_name = *rose; print "$other +_name\n"'