登录网页后传递自动直接页面

时间:2017-08-04 01:43:23

标签: html perl httprequest lwp

我已通过下面的脚本成功登录网页,但网页停留在重定向页面。我不知道如何通过它。以下是我的代码

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

my $URL="http://www.redirect.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'=>'name',
     'password'=>'pass',
]
);



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

if ($resp->is_success) {
    my $res2 = $UA->post($resp->base, []);
    open(OUTFILE1, ">html1.txt"); 
    print OUTFILE1 $res2->decoded_content;

    if ($res2->is_success) {

        if( ($resp->code() >= 200) && ($resp->code() <400) ) {

            open(OUTFILE, ">html.txt");
            binmode(OUTFILE, ":utf8");    
            print OUTFILE $resp->decoded_content;

        }else{
            print "Error: ". $resp->status_line. "\n";
        }
    }
}

这是我得到的输出文件的一部分

<HTML>
    <HEAD>
        <TITLE>

        </TITLE>
    </HEAD>
    <BODY onLoad="document.AUTOSUBMIT.submit();">This page is used to hold your data while you are being authorized for your request.<BR>
    <BR>You will be forwarded to continue the authorization process. If this does not happen automatically, please click the Continue button below.
    <FORM NAME="AUTOSUBMIT" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded" <INPUT TYPE="SUBMIT" VALUE="Continue"></FORM>
    </BODY>
</HTML>

如何通过此自动直接页面到达我想要的网站?根据答案进行了修改但没有输出。

编辑8/7/2017

我尝试simbabque建议和调试方式,打印出$res2中的html1.txt。输出如下所示

<!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.redirect.com">here</a>.</p>
        <hr>
        <address>Apache/2.2.3 (CentOS) Server at www Port 80</address>
</body></html>

我不知道这是什么意思。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

关于这个的有趣部分可能是标题。 普通网站会发出重定向状态代码,例如302 Found。但这显然不是这里的情况,或者他们不需要这个相当奇怪的HTML事物。

HTML内置了JavaScript执行功能。

<BODY onLoad="document.AUTOSUBMIT.submit();">

它告诉浏览器在页面加载时直接提交表单。你的问题是LWP :: UserAgent无法做到这一点,因为它没有JS支持。

但是由于这种情况总会发生,所以围绕它进行编码是微不足道的。您需要做的就是每次登录时都提交该表单。

my $res = $ua->request($req);
if ($res->is_success) {
    my $res2 = $ua->post($res->base, []);
    if ($res2->is_success) {
        ...
    }
}

表单没有参数。唯一的<input>元素是提交按钮,因为它没有name属性,所以它不会显示为参数。该URL可能与您最初提交的URL相同,但它可能已经完成了实际重定向,因此最好使用响应对象的base属性。

我想知道为什么他们让这个过程如此奇怪。它当然不授权任何东西。它可能会设置其他Cookie,例如其中一个营销重定向内容,但是从您展示的内容中看不到。它也不会停止自动化。