【问题标题】:Error: 302 Moved Temporarily错误:302 暂时移动
【发布时间】:2014-07-31 01:19:14
【问题描述】:

当我执行代码时,它会给出 302 Moved Temporarily 错误 如果您发现此代码中有任何错误,请告诉我们。

#!C:\Perl64\bin\perl
print "Content-type: text/html\n\n";

# seraph.pl - search for Codex Seraphinianus on abebooks
use strict;
my $out_file = "result_seraph.html";    # where to save it

use LWP;
my $browser  = LWP::UserAgent->new;
my $response = $browser->post('http://dogbert.abebooks.com/abe/BookSearch',
    # That's the URL that the real form submits to.
    [
        "ph"     => "2",
        "an"     => "",
        "tn"     => "Codex Seraphinianus",
        "pn"     => "",
        "sn"     => "",
        "gpnm"   => "All Book Stores",
        "cty"    => "All Countries",
        "bi"     => "Any Binding",
        "prl"    => "",
        "prh"    => "",
        "sortby" => "0",
        "ds"     => "100",
        "bu"     => "Start Search",
    ]
); 

die "Error: ", $response->status_line, "\n"
    unless $response->is_success;
open( OUT, ">$out_file" ) || die "Can't write-open $out_file: $!";
binmode(OUT);
print OUT $response->content;
close(OUT);
print "Bytes saved: ", -s $out_file, " in $out_file\n";

我可以对其进行哪些更正请告诉我

【问题讨论】:

  • 您尝试对其进行哪些更正?

标签: apache perl http


【解决方案1】:

您使用的代码与 Perl 和 LWP 书中的代码相同:http://lwp.interglacial.com/ch05_06.htm

这是很旧的,同时它引用的 URL 已经移动,这正是错误消息所说的。

要解决此问题,您必须允许重定向,请使用以下代码

my $browser = LWP::UserAgent->new;
push @{$browser->requests_redirectable}, 'POST';

【讨论】:

  • 不是盗版链接。这是作者的网站。当 O'Reilly 将其绝版时,他将它的一个版本放到了网上。
【解决方案2】:

来自the manual

$ua->requests_redirectable(\@requests)

这会读取或设置对象的请求名称列表,$ua->redirect_ok(...) 将允许重定向。默认情况下,这是 ['GET', 'HEAD'],根据 RFC 2616。要更改为包含“POST”, 考虑:

push @{ $ua->requests_redirectable }, 'POST';

【讨论】:

    猜你喜欢
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 2015-07-26
    • 1970-01-01
    相关资源
    最近更新 更多