java - HttpURLConnection 将我的 POST 请求重定向到 GET

标签 java http playframework httpurlconnection playframework-1.x

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

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 header 之一。

仅供引用,我没有直接使用 HttpURLConnection,而是通过 Play Framework 的 WS 包装器。

我的问题是 - 这是 Java (Sun JVM 1.7.0) 的已知问题吗?或者它可能是 Play Framework 中的错误?

最佳答案

这是 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"
     */

关于java - HttpURLConnection 将我的 POST 请求重定向到 GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14246557/

相关文章:

java - 使用 Jsoup (Java) 比较两个 HTML 文档

java - 打开 api 3.0 java 代码生成器 - 如何生成实例化列表?

ajax - 来自 HTTP/REST 服务的进度通知

eclipse - 玩!框架;仅在服务器上编译而不是在客户端上编译

java - 可以使用子类实体查询数据库

java - 使用 hatoas 的休息应用程序 : Multiple markers at this line

java - 在 JSP 页面内调试?

java - 无法使用 Java 的 URLConnection 获取响应 header 位置

java - servlet doGet 和 doPost 方法

scala - 在 Playframework2 中,如何提供多个 Assets 目录?