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();

In reply to Automatically Logging in to a Webpage by awohld

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.