6

我正在发送一个HttpURLConnectionwith setInstanceFollowRedirects(true)and POST,得到一个如下所示的重定向响应:

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 中的错误?

4

2 回答 2

8

这是 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"
     */
于 2013-01-10T10:47:04.373 回答
0

另一种选择,假设您控制服务器:使用状态码 307。

于 2013-01-10T12:13:06.237 回答