CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Redirecting to a URL Using LWP::SIMPLE

 



lsamagana
New User

Sep 4, 2012, 8:04 AM

Post #1 of 4 (1155 views)
Redirecting to a URL Using LWP::SIMPLE Can't Post

Hi,

I am trying to get the contents of the webpage, but I am getting the contents of the login page, its not redirecting to the page once logged in.

Code that I have written

use LWP::UserAgent;
use LWP::Simple;
$username = "abcd";
$password = "xxxxxx";
# Prepare request.
my $ua = new LWP::UserAgent;
my $req = new HTTP::Request(GET => 'http://perlguru.com/gforum.cgi?do%3Dpost_write%3Bforum%3D2=Post+New');
$req->authorization_basic($username, $password);

# Request file.
my $res = $ua->request($req);

# Check if request was successful.
#if ($res->is_success) {
print $res,"\n";
if($res->is_success and $res->previous){
print $req->url, ' redirected to ', $res->request->uri, "\n";
my $resu = get $res->request->uri;
print $resu


When I print this I get the contents of the Login page not the actual page which has been specified.

Can anyone help me in this?


wickedxter
User

Sep 4, 2012, 12:29 PM

Post #2 of 4 (1147 views)
Re: [lsamagana] Redirecting to a URL Using LWP::SIMPLE [In reply to] Can't Post

maybe this will help this will automaticly log you in and return the main forum page... you need to runn this on a web server that can view the html...


Code
use strict; 
use warnings;

use WWW::Mechanize;

my $mech = WWW::Mechanize->new();

#get main site
my $url = 'http://www.perlguru.com';

#username
my $username = '';
#password
my $password = '';


#get main page loaded
$mech->get($url);

#follow link to login screen
$mech->follow_link( n => 2,text_regex => qr/LOG IN|LOGIN|Click here/i );

#get the url we need info from it
my $current_url = $mech->uri();
#get the guest id
$current_url =~ /gforum.cgi\?do=login;guest=(\d+)/;

#Then login
$mech->submit_form(
form_number => 2,
fields => {
login_username => $username,
login_password => $password,
dont_use_cookies => 1,
url => 'http://perlguru.com/gforum.cgi?login_attempt=1',
guest => $1,
}
);

if($mech->success()){
print $mech->content;
}else {
print "Opps some error happend";
}



lsamagana
New User

Sep 4, 2012, 10:52 PM

Post #3 of 4 (1132 views)
Re: [wickedxter] Redirecting to a URL Using LWP::SIMPLE [In reply to] Can't Post

Thank you for the code Smile

I also tried and got some solution.
I am not sure whether the method and the way I am doing is correct or not.


Code
 
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
$mech->requests_redirectable([]);

$mech->requests_redirectable([qw/ GET HEAD POST /]);
$mech->get( 'http://perlguru.com/gforum.cgi?do%3Dpost_write%3Bforum%3D2=Post+New', 'get login' );
$mech->content();
# login
$mech->submit_form(
fields => {
username => 'abcd',
password => 'xxxx',
},
);
$mech->get( 'http://perlguru.com/gforum.cgi?do%3Dpost_write%3Bforum%3D2=Post+New', 'get home' );
my $content = $mech->content();



wickedxter
User

Sep 5, 2012, 5:05 AM

Post #4 of 4 (1102 views)
Re: [lsamagana] Redirecting to a URL Using LWP::SIMPLE [In reply to] Can't Post

you have to login first before your able to post...use the code i gave you to login then you'll have to follow the link to post and you have to submit all the form info even the stuff thats hidden in the HTML source.....

Your not makeing a spam bot are u?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives