Java Spring,使用 POST 重定向到外部 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36353528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:46:07  来源:igfitidea点击:

Spring, redirect to external url using POST

javaspringspring-mvcredirect

提问by Accollativo

In the following Spring 3.1 action, I've to do some stuff and add attribute to a POST request, and then redirect it to external URL through POST (I can't use GET).

在下面的 Spring 3.1 操作中,我必须做一些事情并将属性添加到 POST 请求,然后通过 POST 将其重定向到外部 URL(我不能使用 GET)。

@RequestMapping(value = "/selectCUAA", method = RequestMethod.POST)
public ModelAndView selectCUAA(@RequestParam(value="userID", required=true) String cuaa, ModelMap model) {
    //query & other...
    model.addAttribute(PARAM_NAME_USER, cuaa);
    model.addAttribute(... , ...);
    return new ModelAndView("redirect:http://www.externalURL.com/", model);
}

But with this code the GET method is used (the attributes are appended to http://www.externalURL.com/). How can I use the POST method? It's mandatory from the external URL.

但是在这段代码中使用了 GET 方法(属性被附加到http://www.externalURL.com/)。如何使用 POST 方法?这是来自外部 URL 的强制性要求。

采纳答案by Accollativo

Like @stepanian said, you can't redirect with POST. But there are few workarounds:

就像@stepanian 说的,你不能用 POST 重定向。但是有几个解决方法:

  1. Do a simple HttpUrlConnection and use POST. After output the response stream. It works, but I had some problem with CSS.
  2. Do stuff in your controller and after redirect the result data to a fake page. This page will do automatically the POST through javascript with no user interaction (more details):
  1. 做一个简单的 HttpUrlConnection 并使用 POST。输出响应流后。它有效,但我在使用 CSS 时遇到了一些问题。
  2. 在你的控制器中做一些事情,然后将结果数据重定向到一个假页面。此页面将通过 javascript 自动执行 POST,无需用户交互(更多详细信息):

html:

html:

<form name="myRedirectForm" action="https://processthis.com/process" method="post">
    <input name="name" type="hidden" value="xyz" />
    <input name="phone" type="hidden" value="9898989898" />
    <noscript>
        <input type="submit" value="Click here to continue" />
    </noscript>
</form>
    <script type="text/javascript">

        $(document).ready(function() {
            document.myRedirectForm.submit();
        });

    </script>

回答by stepanian

You can't redirect with POST. You can send a POST request using Java code with a class like HttpURLConnectionwithin the action.

你不能用 POST 重定向。您可以使用 Java 代码在操作中使用HttpURLConnection之类的类发送 POST 请求。