469,343 Members | 5,350 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

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:
Expand|Select|Wrap|Line Numbers
  1. use HTTP::Cookies;
  2. use HTTP::Request;
  3. use HTTP::Request::Common qw(POST);
  4. use URI::URL;
  5. use HTML::LinkExtor;
  6. use LWP 5.64;
  7.  
  8. my $ua = LWP::UserAgent->new();
  9. $ua->cookie_jar({});
  10. ($ua, $status) = login($ua, 'webaddress', 'username', 'password');
  11.  
  12. sub login {
  13.     my ($ua, $login_site, $user_id, $user_pass) = @_;
  14.     $request = POST $login_site, [ 
  15.         username => $user_id,
  16.         passwd   => $user_pass,
  17.         option   => 'login',
  18.         op2      => 'login',
  19.         lang     => 'english',
  20.         return   => 'webaddress',
  21.         message  => '0',
  22.         force_session => '1',
  23.         j3b7d8c5a36287aa77ee125d48b4bd2b2 => '1',
  24.     ];
  25.  
  26.     push @{ $ua->requests_redirectable }, 'POST'; 
  27.     $response = $ua->request($request);
  28.     $status = $response->is_success;
  29.     if ($status) {
  30.         print("Successfully logged in to $login_site\n"); 
  31.     } else {
  32.         print $response->status_line, "\n";
  33.         print "Login Error: ".$response->status_line."\n";
  34.         print "Unable to Login\n";
  35.         print "Unable to login\n";
  36.     }
  37.     return ($ua, $status);
  38. }
  39.  
The output what i get is:
Expand|Select|Wrap|Line Numbers
  1. 301 Moved Permanently
  2. Login Error: 301 Moved Permanently
  3. Unable to Login
  4. Unable to login
  5.  
Below is the form details:
Expand|Select|Wrap|Line Numbers
  1. <form action="webaddress" method="post" name="login" >
  2. Username: <input name="username" id="mod_login_username" type="text" class="inputbox" alt="username" size="10" />
  3. <br />
  4. Password: <input type="password" id="mod_login_password" name="passwd" class="inputbox" size="10" alt="password" />
  5. <br />
  6. <input type="checkbox" name="remember" id="mod_login_remember" class="inputbox" value="yes" alt="Remember Me" />
  7. <label for="mod_login_remember">Remember me</label>
  8. <br />
  9. <input type="submit" name="Submit" class="button" value="Login" />
  10. <a href="webaddress,lostPassword/">    Lost Password?</a>
  11.  
  12. <input type="hidden" name="option" value="login" />
  13. <input type="hidden" name="op2" value="login" />
  14. <input type="hidden" name="lang" value="english" />
  15. <input type="hidden" name="return" value="webaddress" />
  16. <input type="hidden" name="message" value="0" />
  17. <input type="hidden" name="force_session" value="1" />
  18. <input type="hidden" name="j3b7d8c5a36287aa77ee125d48b4bd2b2" value="1" />
  19. </form>
  20.  
Thanks in Advance,
Freedolen
Oct 18 '07 #1
4 7919
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
Oct 22 '07 #2
numberwhun
3,503 Expert Mod 2GB
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
Oct 22 '07 #3
numberwhun
3,503 Expert Mod 2GB
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
Oct 22 '07 #4
miller
1,089 Expert 1GB
Freedolen,

The best advice that I can give you is to use [URL http://search.cpan.org/search?query=WWW::Mechanize]WWW::Mechanize[/url] instead of [URL http://search.cpan.org/search?query=LWP::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. :)

Expand|Select|Wrap|Line Numbers
  1. use Carp;
  2. use WWW::Mechanize;
  3.  
  4. use strict;
  5.  
  6. my $webaddress = 'baz';
  7. my $user_id = 'foo';
  8. my $passwd = 'bar';
  9.  
  10. my $mech = WWW::Mechanize->new(
  11.     cookie_jar      => {},
  12. #    autocheck       => 1,
  13. #    onerror         => \&Carp::croak,
  14. );
  15.  
  16. # Login Form
  17. my $response = $mech->get($webaddress);
  18. if (!$response->is_success) {
  19.     die "Login page unreachable $webaddress: ",  $response->status_line, "\n";
  20. }
  21.  
  22. # Login
  23. $mech->form_name("login");
  24. $mech->field('username', $user_id);
  25. $mech->field('passwd', $passwd);
  26. my $response = $mech->click();
  27. if ($response->is_success) {
  28.     print "Login Successful!\n";
  29. } else {
  30.     die "Login failed: ",  $response->status_line, "\n";
  31. }
  32.  
  33. 1;
  34.  
  35. __END__
  36.  
- Miller
Oct 23 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by joe t. | last post: by
6 posts views Thread by =?Utf-8?B?UGFyYWcgR2Fpa3dhZA==?= | last post: by
6 posts views Thread by Kat | last post: by
9 posts views Thread by Josh | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.