Discussions

Web tier: servlets, JSP, Web frameworks: programatic POST request from servlet?

  1. programatic POST request from servlet? (4 messages)

    Hi,

    I have an application I'm developing that as a certain action has to send a POST request (with parameters) to an external web site (written in php, not under
    my control). Is there a way I can generate this request and forward the browser to the new page from within a servlet/java code to emulate a http form post
    request?

    any pointers gratefully received,

    Jonny
  2. You could have your servlet build and POST the request to the external site. The response from that site could then be sent as a response to your client.

    You wouldnt actually be 'forwarding' the browser to the external site, but POSTing data to it and then displaying the response the servlet receives. The problem with this approach is; if the external sites' response contains links/images with relative URL's in it, they wont work.
  3. Thanks. I was hoping j2ee had some easy way of doing this, but I guess not.

    I've got something to work, and I parse the respose to turn relative links/images
    into absolute pointing to the external site. All works, except a problem with cookies. The external site I'm going to uses cookies to track user sessions. If I turn off cookies on my browser (going directly to the site) I cannot add things to the "cart". I get the same response from my servlet code - as if I'm not receiving cookies, although I am.

    Do I have to send another response back to the external machine to say I received the cookie?

    thanks,

    Jonny
  4. When you get the response from the external site, you should be able to read the cookie from the response header. I guess you could extract it and send it back with the next post.
  5. Jakarta Commons HttpClient[ Go to top ]

    Jakarta Commons HttpClient can sends HTTP POST requests to remote web servers..

    http://jakarta.apache.org/