Tim
Tim

Reputation: 214

Pass through 302 respond and save the html for the redirect page

I have write a code to get the html code from security website. I get the 302 respond but i am not sure how to save the html for the webpage. Below is my code.

#!/usr/bin/perl -w

use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common qw(POST);
use HTTP::Cookies;

my $URL="http://www.example.com";
my $UA = LWP::UserAgent->new();
$UA->ssl_opts( verify_hostnames => 0 ); 


my $req =HTTP::Request::Common::POST("$URL",
   Content_type=>'form-data',
   Content =>[
     'username'=>'user',
     'password'=>'password',
   ]
);
$req->header('Cookie' =>q(TIN=287000; LastMRH_Session=439960f5; MRHSession=78c9c47291c1fcedae166121439960f5));

my $resp=$UA->request($req);

if ($resp->is_success) {

    my $res2 = $UA->post($resp->base, []);
    print $res2->decoded_content;
}

Below is the 302 respond i get

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
    <title>302 Found</title>
</head><body>
    <h1>Found</h1>
<p>The document has moved <a 
    href="http://www.example.com">here</a>.</p>
    <hr>
    <address>Apache/2.2.3 (CentOS) Server at www Port 80</address>
</body></html>

I want to get the html information from the website www.XXX.com but i only can get the 302 respond. I think i stuck in the redirect loop but not sure how to pass trough it.

Upvotes: 1

Views: 440

Answers (1)

Jason
Jason

Reputation: 31

The problem with this is LWP::UserAgent does not allow for the "post" method by default. You need the following code to allow for the POST method:

push @{ $UA->requests_redirectable }, 'POST';

so, like so:

my $UA = LWP::UserAgent->new();
push @{ $UA->requests_redirectable }, 'POST';
$UA->ssl_opts( verify_hostnames => 0 ); 

Upvotes: 3

Related Questions