【问题标题】:How to get rid of HTTP 302 status code while reading a page in Perl?如何在 Perl 中读取页面时摆脱 HTTP 302 状态代码?
【发布时间】:2018-08-15 15:13:01
【问题描述】:

我正在尝试获取需要使用 LWP::UserAgent 进行身份验证的网页内容。但是,我的状态为 302 Found 和“文档已移至此处

我添加了push @{ $uagent->requests_redirectable }, 'GET'; 声明但没有帮助。

下面是我的代码:

use strict;
use warnings;
use LWP::UserAgent;

my $userid = "iamuser";
my $password = "thisispassword";
my $url = "http://some_website-requires-authentication";

my $uagent = new LWP::UserAgent();
push @{ $uagent->requests_redirectable }, 'GET';

my $req = HTTP::Request->new(GET=>"$url");
$req->authorization_basic($userid,$password);

my $response = $uagent->request($req);
print $response->status_line, "\n";
print $response->headers->as_string;

输出:

302 Found
Cache-Control: no-store
Connection: Keep-Alive
Date: Wed, 11 Aug 2018 04:43:10 GMT
Location: HTTP://my_url.website.com/page/bag
Server: website
Content-Length: 239
Content-Type: text/html; charset=iso-8859-1
Client-Date: Wed, 11 0 04:43:11 GMT
Client-Peer: 
Client-Response-Num: 1
Client-SSL-Cert-Issuer: /CGB/ST=Larer Urban/L=Reds/O=COKPO CB Insurabce/CN=DO A  Secure SDL
Client-SSL-Cert-Subject: /C=UKBopp Drive/O=te Incorporated/OU=COPPER/CN=hula.website.com
Client-SSL-Cipher: AES128-SHA
Client-SSL-Socket-Class: IO::Socket::SSL
Client-Warning: Redirect loop detected (max_redirect = 7)
Keep-Alive: timeout=5, max=91
Set-Cookie: blablabla
Strict-Transport-Security: max-age=31536000
Title: 302 Found
X-Frame-Options: DENY

在我做错了什么方面需要帮助? 提前致谢。

【问题讨论】:

  • 默认获取重定向,无需添加。可能你达到了 max_redirect 限制(默认 7)?查看响应,看看它是否真的在重定向?
  • 您确实明白,您正在做的是您将为使用基于 http 标头的身份验证的极少数网站所做的事情,而对于具有登录表单的更常见的网站则不起作用,对吗?
  • @ysth 谢谢。响应说文档已连同 302 状态代码一起移至此处,即使删除 $uagent ->request_redirectable 也无济于事。
  • 请提供响应标头 ($response->headers->as_string)。随意混淆私人信息(用其他词替换私人词),但不要删除任何内容
  • @ikegami 它说授权:基本 Ui0ow00d0dbu3nyKljdidimd== 用户代理:libwww-perl/6.13

标签: perl http http-status-code-302 lwp-useragent


【解决方案1】:

您收到的标题之一是

Client-Warning: Redirect loop detected (max_redirect = 7)

LWP 检测到它处于无尽的重定向链中,并中止。该网站可能依赖于它提供的 cookie 被返回,但您没有为代理创建一个 cookie jar 来允许它这样做。

替换

my $uagent = LWP::UserAgent->new();

my $uagent = LWP::UserAgent->new( cookie_jar => {} );

【讨论】:

    猜你喜欢
    • 2015-12-29
    • 2016-01-11
    • 1970-01-01
    • 2019-10-25
    • 2013-12-10
    • 1970-01-01
    • 2013-06-18
    • 2013-12-11
    • 1970-01-01
    相关资源
    最近更新 更多