4

我送一個HttpURLConnectionsetInstanceFollowRedirects(true)POST,獲得重定向響應,看起來像這樣:HttpURLConnection的我POST請求重定向到一個GET

HTTP/1.1 302 Found 
Server: nginx 
Date: Wed, 09 Jan 2013 20:47:56 GMT 
Content-Type: text/html; charset=utf-8 
Transfer-Encoding: chunked 
Connection: keep-alive 
Status: 302 Found 
Status: 301 Moved Permanently 
Location: http://foo.bar/... 

而且該JVM發送下一個請求是請求GET (到正確的重定向的URL)。它似乎也刪除了我添加到原始請求中的HTTP標頭之一。

僅供參考,我不直接使用HttpURLConnection,而是通過Play Framework的WS包裝器。

我的問題是 - 這是一個Java(Sun JVM 1.7.0)的已知問題?或者它可能是Play Framework中的一個錯誤?

+0

我以爲這是有目的的,如常見的瀏覽器也會這樣做。 –

+0

@JulianReschke - 我正在實現一個API客戶端,這完全打破了它。 – ripper234

回答

5

這是Java的默認行爲。您可以通過設置系統屬性http.strictPostRedirect = true來更改它。

有關詳細信息,請參閱本報價從Java源HttpURLConnection implementation source

/* The HTTP/1.1 spec says that a redirect from a POST 
    * *should not* be immediately turned into a GET, and 
    * that some HTTP/1.0 clients incorrectly did this. 
    * Correct behavior redirects a POST to another POST. 
    * Unfortunately, since most browsers have this incorrect 
    * behavior, the web works this way now. Typical usage 
    * seems to be: 
    * POST a login code or passwd to a web page. 
    * after validation, the server redirects to another 
    *  (welcome) page 
    * The second request is (erroneously) expected to be GET 
    * 
    * We will do the incorrect thing (POST-->GET) by default. 
    * We will provide the capability to do the "right" thing 
    * (POST-->POST) by a system property, "http.strictPostRedirect=true" 
    */ 
0

一種替代,假設你控制服務器:使用狀態碼307

+0

沒有爲我工作,它仍然重定向到GET請求。 –

+0

聽起來像是一個Java錯誤。 –

相關問題