使用 response.sendRedirect 方法在 java 中发送 Post 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21550752/
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-13 09:20:21  来源:igfitidea点击:

Send Post request in java with response.sendRedirect method

javahttp-post

提问by Vamsi Krishna

I want to send a post request in java. I have seen examples for the post request by using Http Client. But i want use sendRedirect method.

我想用java发送一个post请求。我已经看到了使用 Http Client 发布请求的示例。但我想使用 sendRedirect 方法。

For ex, https://processthis.com/process?name=xyz&phone=9898989898

例如, https://processthis.com/process?name=xyz&phone=9898989898

I want to use post request to send those parameters. So, those params will not be visible to any one and at the same time i need to redirect my url to that url as,

我想使用 post 请求来发送这些参数。因此,任何人都看不到这些参数,同时我需要将我的网址重定向到该网址,因为,

response.sendRedirect("https://processthis.com/process");

回答by A Paul

I do not think this is possible. Why do not you use RequestDispatcher. This will work for you. Just set the parameters in request

我不认为这是可能的。为什么不使用RequestDispatcher。这对你有用。只需在请求中设置参数

request.setAttribute("message", "Hello test");
RequestDispatcher dispatcher = servletContext().getRequestDispatcher(url);

OR - The HTTP spec states that all redirects must be in the form of a GET (or HEAD). You can consider encrypting your query string parameters if security is an issue.

或 - HTTP 规范规定所有重定向都必须采用 GET(或 HEAD)的形式。如果安全是一个问题,您可以考虑加密您的查询字符串参数。

OR - another option, set the parameters in your session in servlet, if you have session. Then get it from session after you redirect to the required page.

或 - 另一个选项,如果您有会话,则在 servlet 中的会话中设置参数。然后在重定向到所需页面后从会话中获取它。

回答by Jo?o Sim?es

When a browser receives an HTTP redirect code it will always perform a GET or HEAD to the given url by standard. This is why data must be sent by query strings. If you want to simulate a redirect by POST, you can send to your client a form with the information you want and, on the page load event, you automatically submit the form using Javascript (commonly used to comunicate between different servers, used by SAML protocol for example). Here's an example:

当浏览器收到一个 HTTP 重定向代码时,它总是按照标准对给定的 url 执行 GET 或 HEAD。这就是必须通过查询字符串发送数据的原因。如果你想通过 POST 模拟重定向,你可以向你的客户端发送一个包含你想要的信息的表单,在页面加载事件中,你使用 Javascript 自动提交表单(通常用于不同服务器之间的通信,由 SAML 使用)例如协议)。下面是一个例子:

<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>

Side note: If you already have the information in your server why are you sending a redirect instead of doing the given action? Maybe you want to implement a POST/REDIRECT/GET pattern?

旁注:如果您的服务器中已经有了这些信息,为什么要发送重定向而不是执行给定的操作?也许你想实现一个POST/REDIRECT/GET 模式

回答by Alex

According to RFC2616 with HTTP/1.1 you can send 307response code, which will make user-agentto repeat it's POST request to provided host. In your case just do

根据带有 HTTP/1.1 的 RFC2616,您可以发送307响应代码,这将user-agent重复它对提供的主机的 POST 请求。在你的情况下就做

response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.setHeader("Location", url);

response is your HttpServletResponseobject.

响应是你的HttpServletResponse对象。

Hope this helps.

希望这可以帮助。