0

I create a perl scripts to get some information from the website. The webpage does not redirect itself and i need to click continue myself to redirect it. Can i do it using perl?

#!/usr/bin/perl -w

use feature ':5.10';
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common qw(POST);
use HTTP::Cookies;
use CACertOrg::CA;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;


my $outfile="out.html";
my $URL="http://www.example.com;
my $UA = LWP::UserAgent->new();
$UA->ssl_opts(
    SSL_verify_mode   => 'SSL_VERIFY_NONE',
    verify_hostnames => 0,
    SSL_ca_file      => CACertOrg::CA::SSL_ca_file()
);
$UA->cookie_jar(HTTP::Cookies->new(file => 'cookie_jar', autosave =>1));


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



my $resp=$UA->request($req);
open(OUTFILE, ">$outfile");
print OUTFILE $resp->decoded_content;
close(OUTFILE);

The out.html i print out is like this

enter image description here

when i open the html file, it directly redirect to the page i want but not in the code. Anywhere to do it in the code to reach wwww.example.com.

Added 8/18

I try using this command and it open the www.example.com in my browser

 my $ret = system( 'out.html' );

but what i want is to get the html of www.example.com instead of open the website.

Tim
  • 214
  • 1
  • 12

1 Answers1

0

The best solution is to to set your location header. If it is an option to write output directly to the response (instead of an output HTML file) you can write your headers before the HTML content.

use HTTP::Headers;
my $headers = HTTP::Headers->new;
$headers->header('Content-Type' => 'text/html');
$headers->header('Location' => 'newAddr.html');
$headers->header('Cookie' => q(TIN=287000; LastMRH_Session=439960f5; MRHSession=78c9c47291c1fcedae166121439960f5));
print $headers->as_string();

You can also set your HTTP status code to 302 for a temporary redirect if you want.

Alternatively you could use javascript to automatically redirect after the page has loaded by adding this into your HTML content.

<script type="text/javacript">
    window.location = "newAddr.html";
</script>
elvey
  • 508
  • 5
  • 9
  • Hi @elvey, which part should i add this into my code? And what i want is to get the html code for `www.example.com` and not just enter the website, sorry for the unclear information – Tim Aug 18 '17 at 06:45
  • @Tim Headers are sent by the webserver before the HTML content so they need to go into whatever script is serving out your content. What are you actually doing with out.html? – elvey Aug 19 '17 at 06:29
  • Hi @elvey, the out.html in my scripts is for debug purpose to see whether I success get the html for 'www.example.com' , but unfortunately the out.html I get is the 'continue' as shown above, so I thinking is it a way to click the 'continue' button so I can go get the html for website I want – Tim Aug 19 '17 at 06:55
  • And also if I using the scripts directly open the out.html I get redirect to the page I want in the browser, just not sure why my code cannot get the html code for the page after redirect – Tim Aug 19 '17 at 06:57
  • @Tim To redirect using headers it needs to be sent by the web server before your HTML content, so you can't do it from the HTML document. I've updated my answer to include a javascript redirect which may be a better option if you can't send headers. – elvey Aug 22 '17 at 02:04