Results 1 to 4 of 4

Thread: form post emulation

  1. #1
    Join Date
    Sep 2004
    Posts
    50

    Default 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. #2
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    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
    Darren Davison.
    Public Key: 0xE855B3EA

  3. #3
    Join Date
    Sep 2004
    Posts
    50

    Default

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

  4. #4
    Join Date
    Sep 2004
    Posts
    50

    Default

    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.

Similar Threads

  1. Multiple forms on one view?
    By brianstclair in forum Web
    Replies: 16
    Last Post: May 18th, 2012, 02:42 AM
  2. Replies: 3
    Last Post: Jun 8th, 2010, 03:27 AM
  3. Replies: 9
    Last Post: May 4th, 2006, 09:53 AM
  4. Pre and Post form controllers
    By Paul Taylor in forum Web
    Replies: 4
    Last Post: Apr 13th, 2005, 01:43 AM
  5. Form POST and GET support?
    By netcam in forum Web
    Replies: 2
    Last Post: Mar 12th, 2005, 01:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •