2014-10-10 137 views
3

我有一個Perl腳本,數據發佈到Web服務,我在PHP寫的...錯誤代碼302

這是代碼:

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

當我送的要求,我得到這樣的響應:

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

問題:

  • 我該如何擺脫這個錯誤,或者這是一個錯誤,雖然它說是找到了?
  • 如何獲得帖子的返回值?
  • 發佈數據的正確方法是什麼? (上面的代碼copied from this site。 我的PHP網站所獲得的數據後和回聲,或只是打印出來作爲回報。)提前

感謝。

+2

302是重定向消息。您是否閱讀了[LWP :: UserAgent文檔](http://metacpan.org/pod/LWP::UserAgent)瞭解如何處理重定向? – 2014-10-10 09:56:44

+0

另外 - 如果您的Perl代碼正在被您編寫的PHP腳本讀取,您需要確保PHP腳本在返回響應時做了一些合理的事情。您的PHP腳本是否發送重定向作爲響應,或者您的服務器是否將您的POST請求重定向到其他地方? – 2014-10-10 10:01:05

+0

我猜服務器重定向POST。我的網站託管在託管網站..那麼它只是我的意見。 – waelhe 2014-10-10 10:02:55

回答

3

來自服務器的302錯誤是對客戶端的重定向指令。如果您使用默認配置LWP::UserAgent,它將自動遵循重定向,最多七次。如果你沒有得到一個成功的迴應,這表明你要麼關閉了重定向(這看起來不太可能來自你發佈的代碼,除非你省略了一些LWP::UserAgent的配置細節),或者你得到了卡在重定向循環中。

您可以通過檢查HTTP::Response對象檢查重定向數據:

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放棄。在重定向

更多細節可通過在陣列上下文調用$resp->redirects

# @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一個請求,該請求重定向一次:

# 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腳本中得到預期的響應。

注意:從Perl的5.10啓用say等實用功能,後來,把

use feature ':5.10'; 

在你的腳本的頂部use strict; use warnings;後。