来自 HTTP POST 操作的错误代码 302 [英] Error code 302 from HTTP POST operation

查看:75
本文介绍了来自 HTTP POST 操作的错误代码 302的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 perl 脚本,可以将数据发布到我用 php 编写的 Web 服务...

I have a perl script that posts data to the web service that I wrote in php ...

这是代码:

    use LWP::UserAgent;

    my $ua = LWP::UserAgent->new;

    my $server_endpoint = "http://example.com/";

    my $req = HTTP::Request->new(POST => $server_endpoint);
    $req->header('content-type' => 'application/json');
    $req->header('x-auth-token' => 'kfksj48sdfj4jd9d');

    # add POST data to HTTP request body
    my $post_data = '{ "name": "Dan", "address": "NY" }';
    $req->content($post_data);

    my $resp = $ua->request($req);
    if ($resp->is_success) {
         my $message = $resp->decoded_content;
        print "Received reply: $message\n";
    }
    else {
        print "HTTP POST error code: ", $resp->code, "\n";
        print "HTTP POST error message: ", $resp->message, "\n";
    }

当我发送请求时,我得到了这个响应:

When I send the request, I get this response:

   HTTP POST error code: 302
   HTTP POST error message: Found

问题:

  • 我怎样才能摆脱这个错误,或者这甚至是一个错误,尽管它说找到了?
  • 如何获取帖子的返回值?
  • 发布数据的正确方法是什么?(上面的代码是复制自本站.我的 php 站点获取帖子数据并回显或仅将其打印为返回.)
  • How can I get rid of this error or is this even an error though it's says Found ?
  • How can I get the return value of the post?
  • What is the right way to post data ? (The code above is copied from this site. My php site gets the post data and echo or just print it as return.)

提前致谢.

推荐答案

来自服务器的 302 错误是到客户端的重定向指令.如果您使用 LWP::UserAgent 的默认配置,它将自动跟随重定向最多七次.如果您没有得到成功的响应,则表明您要么关闭了重定向(从您发布的代码中看起来不太可能,除非您省略了 LWP::UserAgentLWP::UserAgent),或者您陷入重定向循环.

A 302 error from a server is a redirection instruction to the client. If you are using the default configuration of LWP::UserAgent, it will automatically follow redirects up to a maximum of seven times. If you are not getting a successful response, it suggests that either you've got redirects turned off (which looks unlikely from the code you've posted, unless you've omitted some configuration details for LWP::UserAgent), or that you're getting stuck in a redirect loop.

您可以通过检查 HTTP::Response 对象来检查重定向数据:

You can examine the redirection data by checking the HTTP::Response object:

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

# check for success, etc.
...

if ($resp->is_redirect) {
    # check the number of redirects that the script has made:
    say "n redirects: " . $resp->redirects;
}

使用默认的 LWP::UA 设置,七个是 LWP::UA 放弃之前您将获得的最大重定向数.

With the default LWP::UA settings, seven is the maximum number of redirects you'll get before LWP::UA gives up.

通过在数组上下文中调用 $resp->redirects 可以获得有关重定向的更多详细信息:

More details on the redirects is available by calling $resp->redirects in array context:

# @redirects is an array of HTTP::Response objects
my @redirects = $resp->redirects;

# print out the 'location' header for each Response object to track the redirection:
say "Location: " . $_->header('location') for @redirects;

# or, for more comprehensive troubleshooting, print out the whole response:
say "Response: " . $_->as_string for @redirects;

请求 google.com 的示例输出,重定向一次:

Example output for a request to google.com, which redirects once:

# say "n redirects: " . $resp->redirects;
n redirects: 1

# say "Location: " . $_->header('location') for @redirects;
Location: http://www.google.co.uk/?gfe_rd=cr&ei=1bg3VJikJ_HH8gfOk4GwDw

# say "Response: " . $_->as_string for @redirects;
Response: HTTP/1.1 302 Found
Cache-Control: private
Connection: close
Date: Fri, 10 Oct 2014 10:45:41 GMT
Location: http://www.google.co.uk/?gfe_rd=cr&ei=1bg3VJikJ_HH8gfOk4GwDw
Server: GFE/2.0
Content-Length: 261
Content-Type: text/html; charset=UTF-8
Alternate-Protocol: 80:quic,p=0.01
Client-Date: Fri, 10 Oct 2014 10:45:39 GMT
Client-Peer: 74.125.230.102:80
Client-Response-Num: 1
Title: 302 Moved

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.uk/?gfe_rd=cr&amp;ei=1bg3VJikJ_HH8gfOk4GwDw">here</A>.
</BODY></HTML>

我的猜测是您陷入了重定向循环,这就是为什么您没有从 PHP 脚本中获得预期响应的原因.

My guess is that you've got stuck in a redirect loop, and that is why you're not getting the expected response back from your PHP script.

注意:要启用 Perl 5.10 及更高版本中的 say 和其他有用的功能,请将

NB: to enable say and other useful features from Perl 5.10 and later, put

use feature ':5.10';

在脚本的顶部 use strict 之后;使用警告;.

这篇关于来自 HTTP POST 操作的错误代码 302的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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