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

I am kinda new to Perl but i am trying, what basically i want to do is login to IITS system and go to particular page parse it and get webpage content.Though i havent reached to the point for parsing i want to go step by step. But I get Response status is: 405 Method not allowed error. Help Needed
#!/usr/bin/perl use LWP::UserAgent; use HTTP::Cookies; use HTTP::Request::Common ; #!/usr/bin/perl use LWP::UserAgent; use HTTP::Cookies; use HTTP::Request::Common ; use HTTP::Request; use HTTP::Response; use HTTP::Headers; use HTML::Parser; use XML::Parser; my $login = { username => 'guestuser', # login form + to post to IITS userPassword => 'temp', ip_address => '10.70.30.20', successURL => 'home.asp', submit => 'login', }; my $ua = LWP::UserAgent->new( keep_alive => 1 ); # timeout => 30 ); my $cookie_jar = HTTP::Cookies->new; # Create a coo +kie store object $cookie_jar->add_cookie_header($request); $ua->cookie_jar( $cookie_jar ); # Associate co +okie store with the user agent my $header = HTTP::Headers->new( Content_Type => 'text/html' +, # Create an HTTP headers object User_Agent => 'Release_Bu +ild/1.0' ); $header->as_string; my $request = HTTP::Request::Common::POST( 'http://10.1.19.22', $l +ogin, User_Agent => 'Relea +se_Build/1.0' ); $request->as_string; my $response = $ua->request( $request ); # Send IITS lo +gin print $response->as_string(); $cookie_jar->extract_cookies($response); # Save session + cookie returned in the response $cookie_jar->as_string(); while ($response->is_redirect) { my $location = $response->header( "location" ); $request = HTTP::Request->new( "GET", "http://10.1.19.22/" . $ +location, $header ); print $request->as_string; $response = $ua->request( $request ); print $response->as_string(); } unless ($response->is_success) { print "Response status is: ", $response->status_line(); return undef; }

Replies are listed 'Best First'.
Re: webpage login
by davis (Vicar) on Jun 29, 2004 at 10:33 UTC
    I tend not to use HTTP::Request directly unless I have to. I use LWP::UserAgent's post method thusly (untested):
    my $ua = LWP::UserAgent->new(keep_alive => 1, requests_redirectable => + [qw/GET POST HEAD/]); my %args = ( foo => "bar", ); my $response = $ua->post($form_url, \%args);
    You might also find that the POST request gets redirected, in which case the requests_redirectable should help.
    If this doesn't fix it, we're going to need more information..

    davis
    It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
Re: webpage login
by gellyfish (Monsignor) on Jun 29, 2004 at 09:02 UTC

    I don't think we are going to be able to help you without being able to see an example of the form that you are trying to submit to - apart from that it looks fine.

    /J\