How to login website using LWP and HTTP modules?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Freedolen
    New Member
    • Mar 2007
    • 17

    How to login website using LWP and HTTP modules?

    Hi All,

    I had a perl script which is used to login in a web page, but it gives the error as
    "301 Moved Permanently". What does this means and how can it be rectified?
    Can anyone help on this?

    Code Snippet:
    [CODE=perl]
    use HTTP::Cookies;
    use HTTP::Request;
    use HTTP::Request:: Common qw(POST);
    use URI::URL;
    use HTML::LinkExtor ;
    use LWP 5.64;

    my $ua = LWP::UserAgent->new();
    $ua->cookie_jar({}) ;
    ($ua, $status) = login($ua, 'webaddress', 'username', 'password');

    sub login {
    my ($ua, $login_site, $user_id, $user_pass) = @_;
    $request = POST $login_site, [
    username => $user_id,
    passwd => $user_pass,
    option => 'login',
    op2 => 'login',
    lang => 'english',
    return => 'webaddress',
    message => '0',
    force_session => '1',
    j3b7d8c5a36287a a77ee125d48b4bd 2b2 => '1',
    ];

    push @{ $ua->requests_redir ectable }, 'POST';
    $response = $ua->request($reque st);
    $status = $response->is_success;
    if ($status) {
    print("Successf ully logged in to $login_site\n") ;
    } else {
    print $response->status_line, "\n";
    print "Login Error: ".$response->status_line."\ n";
    print "Unable to Login\n";
    print "Unable to login\n";
    }
    return ($ua, $status);
    }
    [/CODE]

    The output what i get is:
    Code:
    301 Moved Permanently
    Login Error: 301 Moved Permanently
    Unable to Login
    Unable to login
    Below is the form details:
    [CODE=html]
    <form action="webaddr ess" method="post" name="login" >
    Username: <input name="username" id="mod_login_u sername" type="text" class="inputbox " alt="username" size="10" />
    <br />
    Password: <input type="password" id="mod_login_p assword" name="passwd" class="inputbox " size="10" alt="password" />
    <br />
    <input type="checkbox" name="remember" id="mod_login_r emember" class="inputbox " value="yes" alt="Remember Me" />
    <label for="mod_login_ remember">Remem ber me</label>
    <br />
    <input type="submit" name="Submit" class="button" value="Login" />
    <a href="webaddres s,lostPassword/"> Lost Password?</a>

    <input type="hidden" name="option" value="login" />
    <input type="hidden" name="op2" value="login" />
    <input type="hidden" name="lang" value="english" />
    <input type="hidden" name="return" value="webaddre ss" />
    <input type="hidden" name="message" value="0" />
    <input type="hidden" name="force_ses sion" value="1" />
    <input type="hidden" name="j3b7d8c5a 36287aa77ee125d 48b4bd2b2" value="1" />
    </form>
    [/CODE]
    Thanks in Advance,
    Freedolen
    Last edited by miller; Oct 18 '07, 08:13 PM. Reason: Code Tag and ReFormatting
  • Freedolen
    New Member
    • Mar 2007
    • 17

    #2
    Since i didn't get reply for the past four days, i have a doubt that my question is not understandable? or shall i post to some other forums(i am asking this b'cos i saw from some other posts as 'dont post the same question to many forums')

    -Freedolen

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Originally posted by Freedolen
      Since i didn't get reply for the past four days, i have a doubt that my question is not understandable? or shall i post to some other forums(i am asking this b'cos i saw from some other posts as 'dont post the same question to many forums')

      -Freedolen
      My appologies for you question not receiving any attention. I am a little swamped at the moment but will take a look later tonight and see if I can offer you any help, even if anyone else doesn't post.

      Regards,

      Jeff

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        I took a look, and according to this web site, it looks like it has something to do with the URL you are entering. Make sure that it is correct.

        Regards,

        Jeff

        Comment

        • miller
          Recognized Expert Top Contributor
          • Oct 2006
          • 1086

          #5
          Freedolen,

          The best advice that I can give you is to use [URL http://search.cpan.org/search?query=WW W::Mechanize]WWW::Mechanize[/URL] instead of [URL http://search.cpan.org/search?query=LW P::UserAgent]LWP::UserAgent[/URL]. Then simply be sure to add error checking at each step of your spidering process. Outputting the full content of each page is generally a good practice during development. And then cross your fingers that they don't ever change their html form. :)

          [CODE=perl]
          use Carp;
          use WWW::Mechanize;

          use strict;

          my $webaddress = 'baz';
          my $user_id = 'foo';
          my $passwd = 'bar';

          my $mech = WWW::Mechanize->new(
          cookie_jar => {},
          # autocheck => 1,
          # onerror => \&Carp::croak ,
          );

          # Login Form
          my $response = $mech->get($webaddres s);
          if (!$response->is_success) {
          die "Login page unreachable $webaddress: ", $response->status_line, "\n";
          }

          # Login
          $mech->form_name("log in");
          $mech->field('usernam e', $user_id);
          $mech->field('passwd' , $passwd);
          my $response = $mech->click();
          if ($response->is_success) {
          print "Login Successful!\n";
          } else {
          die "Login failed: ", $response->status_line, "\n";
          }

          1;

          __END__
          [/CODE]

          - Miller

          Comment

          Working...