-1

I want to do the same functionality in the HttpPost, using servlets that is, instead of creating the request using HttpPost, I want to use another request coming from a servlet and change body before forwarding it to the URL "www.url.com/cgi-bin", how can I change the body content of a request ?

public void call() throws ClientProtocolException, IOException, InterruptedException {

        String url = "www.url.com/cgi-bin"
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpPost httppost = new HttpPost(url);
        String data = "body data";
        InputStream stream = new ByteArrayInputStream(data.getBytes("UTF-8"));
        InputStreamEntity reqEntity = new InputStreamEntity(stream, -1);
        reqEntity.setChunked(true);
        httppost.setEntity(reqEntity);
        httppost.addHeader("charset", "utf-8");
        httppost.setHeader("Content-Type", "text/xml");
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
        httpclient.getConnectionManager().shutdown();
    }

I want it to be like...

@WebServlet("/myServlet/*")
    public class MyHandler extends HttpServlet {

        public void doGet(HttpServletRequest request,HttpServletResponse response) {
            // add data to request here ...
            // forward request to the URL ...
        }
    }
Exorcismus
  • 2,243
  • 1
  • 35
  • 68
  • Which data do you want to add? How would you like it to be handled in the view? – Yassin Hajaj Dec 07 '15 at 14:23
  • I want to do the same functionality as in the HttpPost but using the servlet, the data I want to add is just string, I want to add it in the body – Exorcismus Dec 07 '15 at 14:25
  • I do not know how `HttpPost` works but if you want to add an attribute to a request using `HttpServletRequest` you could use `HttpServletRequest#setAttribute(String, Object)` – Yassin Hajaj Dec 07 '15 at 14:27
  • Have a look at the Servlet Filter and see whether you can do it there. You can definitely inject headers and parameters. Not sure about the actual body though. – David Brossard Dec 07 '15 at 17:42
  • Possible duplicate of [How to use a servlet filter in Java to change an incoming servlet request url?](http://stackoverflow.com/questions/2725102/how-to-use-a-servlet-filter-in-java-to-change-an-incoming-servlet-request-url) – David Brossard Dec 07 '15 at 17:42
  • Unsure of what you want. Do you want to change request and forward it *to same application* => use a filter, or do you want to process request and pass request data to another server => use Apache HttpClient? – Serge Ballesta Dec 07 '15 at 18:14
  • Can you explain more your question ? It is not clear if you want to make a forward to a servlet inside your application or if it is outside yours. – reos Dec 07 '15 at 18:45

1 Answers1

0

Unfortunately it is not possible, using servlet api's, for a servlet to generate a new post request with body content.

mmulholl
  • 281
  • 2
  • 7
  • I am not sure this is actually true... I believe there are ways e.g. via a Servlet Filter. Can you substantiate your claim? – David Brossard Dec 07 '15 at 17:44
  • 1
    You could write a HttpServletRequestWrapper to override methods like getInputStream(), getHeader() and getMethod() to make it look like a post request to the forwarded resource, but you can't create a new request. – mmulholl Dec 07 '15 at 17:48