473,385 Members | 1,546 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,385 software developers and data experts.

How to handle Webpage redirection in LWP Perl

The following is my very simple script :

</script>

#!/usr/bin/perl
use strict;
use LWP::UserAgent;
my $url = "<My Web Server IP : PORT Number>";

my $browser = LWP::UserAgent->new(keep_alive => 5, timeout => 10, agent => 'unimportant',);
$browser->timeout(10);

my @fields = [user => '<My UserName>',
password => '<My Password>'
];
my $response = $browser->post($url,@fields);

#if ($response->is_error())
#{
#printf "Error!";
#printf "%s\n", $response->status_line;
#}
my $contents = $response->content();
print "---------------------------------------------","\n";
print $contents;
print "---------------------------------------------","\n";

if ($response->is_redirect){
print "Redirect","\n";
}

</script>

THE ABOVE SCRIPT FAILS

My problem is this:

I have to login to a web server (basically i get a form upon typing the ip address on the web browser, which asks a user name and password). It is done using the perl LWP itself. But, the main problem is this:

Upon logging in to the website, it redirects to another location (in the same IP Address itself). So, i am not able to access the redirected webpage. How do i do it?

How do i successfully login and access the main page of a webserver which redirects me upon login ?

:mad:
Jun 29 '06 #1
3 12005
sorry for just putting the question and not even asking for a reply :

I kindly request anyone among you to guide me regarding what has to be done
Jun 29 '06 #2
hi

i've got the same problem

have you found a solution?
Feb 19 '07 #3
well, it wasn't that problematic to do.

let's imagine, we have a web-site with authentication form. i could look like this:
Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <FORM method="post" action="letMeIn.php">
  3. <table border="5" cellpadding ="50">
  4. <TR><TD>
  5. login:<br/>
  6. <INPUT type="text" name="login"><br/>
  7. password:<br/>
  8. <INPUT type="password" checked name="passwwd"><br/>
  9. <INPUT type="submit" value="submit">
  10. </TD></TR></table></center></FORM>
  11. </body>
  12.  
we can see: FORM method="post", INPUT name="login", INPUT name= "passwd". so let's just POST 'login' and 'passwwd' to letMeIn.php, write down a cookie, and then GET a side with content

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. use LWP::UserAgent;
  4. use HTTP::Cookies;
  5. $ua = LWP::UserAgent->new;
  6. $ua->agent("MyApp/0.1 ");
  7.  
  8. # set a cookie stuff. 1 line, nothing more is needed  
  9. $ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt", autosave => 1));
  10.  
  11. # post login and password to appropriate script
  12. $req = HTTP::Request->new(POST => 'http://somewhere.pl/logs/letMeIn');
  13. $req->content_type('application/x-www-form-urlencoded');
  14. $req->content('login=iv&&passwwd=iv_ext');
  15. my $res = $ua->request($req);
  16.  
  17. # check the outcome of the response
  18. if ($res->is_success) {
  19.     print "success\n";
  20.     print $res->content;
  21. } else {
  22.     print $res->status_line, "\n";
  23.     print $res->as_string;
  24.     print "ok\n", $res->is_redirect if $res->is_redirect;
  25.  
  26. # and we'll get here the following:
  27.  
  28. #302 Found
  29. #HTTP/1.1 302 Found
  30. #Cache-Control: no-store, no-cache, must-revalidate, post-check=0, #pre-check=0
  31. #Connection: close
  32. #Date: Mon, 19 Feb 2007 09:15:19 GMT
  33. #Pragma: no-cache
  34. #Location: mainFrame.php
  35. #Server: Apache/1.3.33 (Debian GNU/Linux) PHP/4.3.10-18 #mod_ssl/2.8.22 OpenSSL/0.9.7e mod_perl/1.29
  36. #Content-Type: text/html
  37. #Expires: Thu, 19 Nov 1981 08:52:00 GMT
  38. #Client-Date: Mon, 19 Feb 2007 09:15:20 GMT
  39. #Client-Peer: 62.69.206.66:80
  40. #Client-Response-Num: 1
  41. #Client-Transfer-Encoding: chunked
  42. #Set-Cookie: PHPSESSID=d18695cd0179c4dffa6452937ca1a4d2; path=/
  43. #X-Powered-By: PHP/4.3.10-18
  44. #
  45. #ok
  46.  
  47. # see that? $res->status_line gives us 302, which means redirection, 
  48. #$res->is_redirect admits that, and $res->as_string gives us Location: 
  49. #mainFrame.php. so let's just get this
  50.     $req1 = HTTP::Request->new(GET => 'http://somewhere.pl/logs/mainFrame.php');
  51.     $req1->header('Accept' => 'text/html');
  52.     my $res1 = $ua->request($req1);
  53.     print $res1->as_string;
  54.  
  55. # and here i get what i want. 
  56.  
  57. }
  58.  
hope it was helpful

iv
Feb 19 '07 #4

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

Similar topics

6
by: MadHatter | last post by:
Hello, is there any command line switch or anything else which would allow me to use the standard input as the source for a script? so that I could type perl hit return and type out the script...
12
by: David Walker | last post by:
Hi I have a program which I need to interface with a webpage - the webpage will accept an input (probably a 'post' string from the program) and then will process it and needs to return a value. ...
1
by: slash | last post by:
Hi, I have a page on my website that is driven dynamically off of a database by a perl/cgi script. The page contents are essentially reports of the following url:...
8
by: Manu | last post by:
Hi there, I am using a perl script to generate this html page which just redirects users to my ftp server. The problem is I get a "page cannot be displayed" in IE 6.0 (haven't tried other...
22
by: Ruslan Kogan | last post by:
Hi, I have a webpage with a table that has 4 cells (2x2). In one of those cells, I wish to load a webpage stored on a different server (say...www.webpage.com/abc.htm). Does HTML allow for this...
2
by: prajil | last post by:
Hi, Could anyone point how can i override output redirection using perl. i.e. command > file 2>&1 will redirect both output and error of command to file I need to print a message to...
5
by: thelevitator | last post by:
Im trying to execute a perl script from a shell script. I have to pass the path of a file in the command line when i execute the perl script (Automate the perl script execution). How do i feed the...
0
by: snsanju | last post by:
Hi, i am writing a program to fetch the portion of the data of a webpage. Here i am using www::mechanize module. I got succeeded to print the part of the web page section but here the problem...
1
by: gnidoc | last post by:
This is a small perl script I wrote to check mirror lists (files filled with one URL per line). It prints " http://..." when an URL can't be retrieved and just prints the url when everything goes...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.