登录后传递自动直接页面到网页 [英] Pass auto direct page after login to the webpage

查看:169
本文介绍了登录后传递自动直接页面到网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  use strict; 
使用警告;
使用LWP :: UserAgent;
使用HTTP :: Request :: Common qw(POST);
使用HTTP :: Cookies;

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

$ b my $ req = HTTP :: Request :: Common :: POST($ URL,
Content_type =>'form-data',
Content => [
'username'=>'name',
'password'=>'pass',
]
);



my $ resp = $ UA-> request($ req);
$ b $ if($ resp-> is_success){
my $ res2 = $ UA-> post($ resp-> base,[]);
open(OUTFILE1,> html1.txt);
打印OUTFILE1 $ res2-> decoded_content; ($ resp-> code()> = 200)&&& $($ resp-1)

if($ res2-> is_success){

> code()< 400)){

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

}其他{
打印错误:。 $ resp-> status_line。 \\\
;
}
}
}

这是输出文件我得到

 < HTML> 
< HEAD>
< TITLE>

< / TITLE>
< / HEAD>
< BODY onLoad =document.AUTOSUBMIT.submit();>当您获得您的请求授权时,此页面用于保存您的数据。< BR>

您将被转发以继续授权过程。如果这不会自动发生,请点击下面的继续按钮。
< FORM NAME =AUTOSUBMITMETHOD =POSTENCTYPE =application / x-www-form-urlencoded< INPUT TYPE =SUBMITVALUE =Continue>< / FORM>
< / BODY>
< / HTML>

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



编辑8/7/2017



我试试 simbabque 通过打印 html1.txt 中的 $ res2 来建议和调试。输出如下所示:

 <!DOCTYPE HTML PUBLIC -  // IETF // DTD HTML 2.0 // EN> ; 
< html>< head>
< title> 302找到< / title>
< / head>< body>
< h1>找到< / h1>
< p>文件已移至< a
href =http://www.redirect.com>此处< / a>。< / p>
< hr>
<地址>在www端口80上的Apache / 2.2.3(CentOS)服务器< / address>
< / body>< / html>

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

解决方案

有趣的部分可能是标题。 正常网站会发出重定向状态码,例如 302 Found 。但这显然不是这种情况,或者他们不需要这个非常奇怪的HTML事情。



HTML内置JavaScript执行。

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

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



但是,由于这种情况一直发生,所以代码很简单。您只需在每次登录时提交该表单。

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

表单没有参数。这里唯一的< input> 元素是submit按钮,并且它没有名称属性它不会显示为参数。该URL可能与您最初提交的URL相同,但它可能已经完成了真正的重定向,所以最好使用响应对象的 base 属性。 / p>




我不知道为什么他们让这个过程变得很奇怪。它当然不授权任何东西。它可能会设置额外的Cookie,就像其中一种营销重定向方式,但从您展示的内容中看不到。它也不会停止自动化。


I have successful login to a webpage by the script below but the webpage stuck at the redirect page. I has no idea how to pass through it. Below is my code

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";
        }
    }
}

This is the part of the output file i get

<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>

How to pass through this auto direct page to reach the website i want? Have modified according to the answer but nothing was output.

Edit 8/7/2017

I try the way simbabque suggest and debug by print out the $res2 in html1.txt. The output look like below

<!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>

I have no idea what this mean. Can anyone help?

解决方案

The interesting part about this is probably the headers. A normal website would issue a redirect status code, like a 302 Found. But that's clearly not the case here, or they wouldn't need this rather strange HTML thing.

The HTML has built-in JavaScript execution.

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

It tells the browser to submit the form directly when the page has loaded. Your problem is that LWP::UserAgent cannot do that, because it doesn't have JS support.

But since this always happens, it's trivial to code around it. All you need to do is submit that form every time you log in.

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

The form has no parameters. The only <input> element there is is the submit button, and since that doesn't have a name attribute it does not show up as a parameter. The URL is likely the same as the one you submitted originally, but it might have done a real redirect already, so it's better to use the base attribute of the response object.


I do wonder why they make this process so weird. It certainly doesn't authorize anything. It might set additional cookies, like one of those marketing redirect thingies, but that's not visible from what you've showed. And it does not stop automation either.

这篇关于登录后传递自动直接页面到网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆