我已經通過下面的腳本成功登錄到網頁,但網頁卡在重定向頁面。我不知道如何通過它。下面是我的代碼登錄網頁後通過自動直接頁面
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>
如何通過本次車展直接頁訪問的網站,我想?根據答案進行了修改,但沒有輸出。
編輯2017年8月7日
我嘗試打印出html1.txt
的$res2
方式simbabque
建議和調試。輸出類似於下面
<!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>
我不知道這是什麼意思。誰能幫忙?
您在輸出中缺少一個用於打開FORM格式標籤的'>'。這是真的錯過了,還是在複製時丟失了它? – simbabque
這是一個正確的302響應。它告訴你它已被重定向。您的LWP用戶代理應該自動執行。 – simbabque
謝謝@simbabque,如果成功爲什麼程序不能進入'if($ res2-> is success)'循環?該代碼不會打印'html.txt'。 – Tim