473,378 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 8559
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,509 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,509 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

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

Similar topics

4
by: 23s | last post by:
I had this problem in the past, after a server reformat it went away, and now after another server reformat it's back again - no clue what's doing it. Here's the flow: Website root is public, no...
7
by: TJoker .NET | last post by:
I'm developing an VB.NET Windows Forms application that uses CR for VS.NET (original version shipped with VS.NET 2002 - my VS.NET has the latest SP installed, no SPs for CR). My reports get their...
3
by: Brian F | last post by:
Ok here goes: WEBSITE main directory allows anonymous users fine. SITEA, SITEB, and SITEC are all subdirectories under WEBSITE that refer to graphics and javascript in the main virtual...
6
by: joe t. | last post by:
The subject may sound a little cryptic, so i'll try my best to explain. Details are unavailable, as i am under a nondisclosure agreement, but i'm looking for general principles and tips, not...
6
by: =?Utf-8?B?UGFyYWcgR2Fpa3dhZA==?= | last post by:
Hi All, We have a requirement where we have to develop a custom Login Page which will accept user's NT credentials ( Username , password, domain name). This then needs to be passed to a website...
6
by: Kat | last post by:
Every time I attempt to run a localhost website, it asks me for a login, as if I am not a user on the local machine. I am a user on the local machine, I am an admin on the local machine. I am not...
5
by: rockdale | last post by:
Hi, all: I have a website with its own login page. Now one of my clients want their employees log into my website from their website. They want to have their login page (look and feel are...
19
by: klenwell | last post by:
Another request for comments here. I'd like to accomplish something like the scheme outlined at this page here: http://tinyurl.com/3dtcdr In a nutshell, the form uses javascript to hash...
9
by: Josh | last post by:
I run a Joomla website and am familiar with php in some but not all aspects. Currently I am trying to find some solutions related to session handling. Am I correct in saying that "login" is kept...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.