Announcement Announcement Module
Collapse
No announcement yet.
form post emulation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • form post emulation

    I have a form which is being redirected to another website on submission. This form is auto-submitted when the page is loaded.

    I want to replace this form altogether and have the controller create the form contents and submit it to the other website. I do not want the user to see the form.

    I do not think RedirectView will work since it puts all the parameters into the request string, I want to emulate a POST.

    What is the best way to do this?

  • #2
    you can use standard network classes like URLConnection, but you'll find it much easier with something like HttpUnit.

    This isn't really a Spring issue.

    Regards

    Comment


    • #3
      Thanks and sorry for the incorrect posting I just thought there might be an easy solution within Spring.

      Comment


      • #4
        Hopefully this will help someone with a similar problem. I solved this using org.apache.commons.httpclient which comes with Spring and dependencies in the lib/jakarta-commons and package commons-httpclient.jar.

        Here is some sample code:

        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        PostToOtherSite(request, response);

        return null;
        }

        private void PostToOtherSite(HttpServletRequest request, HttpServletResponse response) {

        NameValuePair langNVP = new NameValuePair("lang", "en");
        NameValuePair nameNVP = new NameValuePair("name", "Paul");

        // Create an instance of HttpClient.
        HttpClient client = new HttpClient();

        // Create a method instance.
        PostMethod method = new PostMethod("http://someWebsite");

        // Provide custom retry handler is necessary
        DefaultMethodRetryHandler retryhandler = new DefaultMethodRetryHandler();
        retryhandler.setRequestSentRetryEnabled(false);
        retryhandler.setRetryCount(3);
        method.setMethodRetryHandler(retryhandler);

        method.setRequestBody(
        new NameValuePair[] {langNVP, nameNVP});
        try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
        logger.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody();

        // Output the response to the browser.
        ServletOutputStream os = response.getOutputStream();
        os.print(new String(responseBody));
        os.flush();
        os.close();

        } catch (IOException e) {
        logger.error("Failed to download file." + e.getMessage());
        } finally {
        // Release the connection.
        method.releaseConnection();
        }
        }


        Alot of this was extracted from the apache jakarta website.

        Comment

        Working...
        X