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

I'm trying to automate logging into a webpage.

You first go to http://mysite.com/ and it redirects you to a https page where the URL is encoded with some encrypted string. You the post your username to a script on the page, then the same script sends you a new page and asks you for your password before it lets you in. This site needs you to use cookies.

My question is:
1. Is my virtual browser keeping the "referrer" in the head as the previous pages I visited? IE, is that crazy string in the URL of the first page I was redirected to set as my Referrer?

2. Am I grabbing and keeping my cookies properly?

3. I'm getting Error: "302 Moved" after I enter my password. 302 States: If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued."

So how do I allow the LWP virutal browser I created to automatically redirect instead of erroring out. If I remove the Die code the content doesn't update
#!/usr/bin/perl -w use strict; use LWP; my $site0 = "http://mysite.com/ my $site = "https://login.mysite.com/dir/auth"; my @netscape_headers = ( # Set headers to look like Netscape 'User-Agent' => 'Mozilla/4.76 [en] (Win98; U)', 'Accept-Language' => 'en-US', 'Accept-Charset' => 'iso-8859-1,*,utf-8', 'Accept-Encoding'=> 'gzip', 'Accept' => "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, +image/png, */*", ); my $browser = LWP::UserAgent->new(); # Create virtual browser $browser->cookie_jar( {} ); # Enable cookies my $response = $browser->get($site0, @netscape_headers); die "Error: ", $response->status_line unless $response->is_success; my $response = $browser->post( $site, [ "pRedirect" => "", "fRedirect" => "", "SafeWordUser" => "username" ] ); die "Error: ", $response->status_line unless $response->is_success; print $response->content(); $response = $browser->post( $site, [ "pRedirect" => "", "fRedirect" => "", "SafeWordPassword" => "password" ] ); die "Error: ", $response->status_line unless $response->is_success; print $response->content();

Any other suggestions for this scipt would be apprecieated. The only reason I print response->content() is for debugging purposes.


UPDATE:

Here's my new code, it logs in to the main page okay. To get arround the "302 Moved" error I got rid of the "die" code and manually went to a secured page. If you use a normal browser a pop-up window comes up instead of the 302 error, the browser asks if you want to view non secure elements. I don't know how to automate this into the script.
#!/usr/bin/perl -w use strict; use LWP; my $site = "https://login.mysite.com/dir/auth"; my $site1 = "https://www.mysite.com/protected/dir/main.htm"; my @netscape_headers = ( # Set headers to look like Netscape 'User-Agent' => 'Mozilla/4.76 [en] (Win98; U)', 'Accept-Language' => 'en-US', 'Accept-Charset' => 'iso-8859-1,*,utf-8', 'Accept-Encoding'=> 'gzip', 'Accept' => "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, +image/png, */*", ); my $browser = LWP::UserAgent->new(); # Create virtual browser $browser->cookie_jar( {} ); # Enable cookies my $response = $browser->post( $site, [ "pRedirect" => "", "fRedirect" => "", "User" => "username" ],@netscape_headers ); die "Error: ", $response->status_line unless $response->is_success; $response = $browser->post( $site, [ "pRedirect" => "", "fRedirect" => "", "Password" => "password" ],@netscape_headers ); $response = $browser->get($site1, @netscape_headers); print $response->content();

Replies are listed 'Best First'.
Re: Automatically Logging in to a Webpage
by Vinzzz (Initiate) on May 24, 2005 at 09:12 UTC

    Hi Monk Awohld,
    I'm working on a LWP Virtual agent too. And I get a problem with "refresh page" (Have a look on other post ;)).
    For the cookies purpose. I think you have to complete your code.

    cookie_jar => HTTP::Cookies->new( file => 'mycook.txt', autosave => 1, ignore_discard => 1, )

    The "autosave" option is used by LWP to update the cookie session automatically. The txt file is a "Cookie box". When the HTTP::Cookies will be destroy, it 'll be save in this file (And restore cookie).
    The"ignore_discard" parameter save even cookies that are requested to be discarded
    (Because LWP respect the RFC 2965 -> that means LWP don't discard the cookies when the script ends.)

    If you have use manually your site, maybe your browser have save the cookie sessions. You can import hime by using specific module like :
    HTTP::Cookies::Netscape, HTTP::Cookies::Mozilla etc

    Finnaly, maybe have a look on the HTTP::Status .
    I hope that'll help you. I'm very interresting in your code, i'm begginner in the part of Perl lib.
      I'm going to leave the cookie field blank, so the cookies will be destroyed when the program finishes. I want a fresh login every time.

      My Virtual Agent isn't redirecting either when it gets the 302 Moved code. According to my understanding of the RFC 2616, if a user is POST'ing data and the server sends 302, the browser is supposed to ask for user confirmation that they want to be redirected.

      So I guess this is why the Virtual Agent isn't redirecting.

      Any ideas on how to fix that?
Re: Automatically Logging in to a Webpage
by Anonymous Monk on May 24, 2005 at 09:36 UTC
    So how do I allow the LWP virutal browser I created to automatically redirect instead of erroring out. If I remove the Die code the content doesn't update
    See requests_redirectable in documentation, or do a get instead of die.
Re: Automatically Logging in to a Webpage
by Anonymous Monk on May 24, 2005 at 09:32 UTC
    mysite.com says it supports POP protocol, so why not try something like Net::POP3 instead?
      mysite.com was just a bogus name, this is for work and I probably would be violating company policy by publishing actual info.

        For future reference, the canonical example domain is example.com; it's owned by IANA and reserved by RFC 2606.