Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Post request in java with response.sendRedirect method

Tags:

java

http-post

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.

For ex, 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,

response.sendRedirect("https://processthis.com/process");
like image 603
Vamsi Krishna Avatar asked Feb 04 '14 11:02

Vamsi Krishna


People also ask

What is the use of sendRedirect () method?

sendRedirect() method redirects the response to another resource, inside or outside the server. It makes the client/browser to create a new request to get to the resource. It sends a temporary redirect response to the client using the specified redirect location URL.

What is the difference between sendRedirect () and forward ()?

SendRediret() When we use the forward method request is transferred to other resources within the same server for further processing. In case of sendRedirect request is transferred to another resource to a different domain or different server for further processing.

What is difference between ServletResponse sendRedirect () and RequestDispatcher forward () method?

This is also called server side redirect. A RequestDispatcher forward() is used to forward the same request to another resource whereas ServletResponse sendRedirect() is a two step process. In sendRedirect(), web application returns the response to client with status code 302 (redirect) with URL to send the request.


2 Answers

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

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

response is your HttpServletResponse object.

Hope this helps.

like image 104
Alex Avatar answered Sep 20 '22 17:09

Alex


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:

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

like image 25
João Simões Avatar answered Sep 18 '22 17:09

João Simões