Announcement Announcement Module
Collapse
No announcement yet.
Send HTTP POST request from a spring controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Send HTTP POST request from a spring controller

    Hi,

    im using spring 3.

    i have a special requirement in my project. I need to send(forward) HTTP post request from one of my spring controllers to a external servlet,(servlet only processes post-requests) and returns its result, a jsp page.

    is it possible to send/forward a post request from a spring controller and show the result (jsp) from this servlet ?

    i have tried to create and send the http post from my spring controller with apache-httpcomponents .

    It sends the request, but the problem is that im forwarded to the response from my spring controller, and not the servlet jsp. Is it possible to send the http-post to the servlet, and show its jsp-result ?

    Here is the code i tried:
    Code:
    	@RequestMapping(value = "/runmytestmethod/{id}", method = RequestMethod.GET)
    	public void getRunReportPage(@PathVariable Integer id, Model model,HttpSession session,HttpServletRequest request) {
    		DefaultHttpClient httpclient = new DefaultHttpClient();
    		HttpPost httpPost = new HttpPost("http://targethost/processfurther");
    		httpPost.setEntity(new UrlEncodedFormEntity(nvps));
    		HttpResponse response2 = httpclient.execute(httpPost);
    
    		try {
    		    System.out.println(response2.getStatusLine());
    		    HttpEntity entity2 = response2.getEntity();
    		    // do something useful with the response body
    		    // and ensure it is fully consumed
    		    EntityUtils.consume(entity2);
    		} finally {
    		    httpPost.releaseConnection();
    		}
    }

    all help appreciated...thanks.
    Last edited by bashirshaz; Dec 9th, 2012, 03:52 PM.

  • #2
    You have to write the result from the call to the response not sure what you are exactly trying to do but if you want to show something to the user you will have to write to the response yourself. If I'm not mistaken the response of the httpclient can be read with an inputstream/reader and the HttpServletResponse has a OutputStream/Writer so you can copy from input to output.

    Comment


    • #3
      Spring post to external url

      "Bashirshaz,
      Did you ever figure out how to do a form post to an external url and get a response? I have a similar scenario and need assistance.
      Thank you
      Johan
      "

      Originally posted by bashirshaz View Post
      Hi,

      im using spring 3.

      i have a special requirement in my project. I need to send(forward) HTTP post request from one of my spring controllers to a external servlet,(servlet only processes post-requests) and returns its result, a jsp page.

      is it possible to send/forward a post request from a spring controller and show the result (jsp) from this servlet ?

      i have tried to create and send the http post from my spring controller with apache-httpcomponents .

      It sends the request, but the problem is that im forwarded to the response from my spring controller, and not the servlet jsp. Is it possible to send the http-post to the servlet, and show its jsp-result ?

      Here is the code i tried:
      Code:
      	@RequestMapping(value = "/runmytestmethod/{id}", method = RequestMethod.GET)
      	public void getRunReportPage(@PathVariable Integer id, Model model,HttpSession session,HttpServletRequest request) {
      		DefaultHttpClient httpclient = new DefaultHttpClient();
      		HttpPost httpPost = new HttpPost("http://targethost/processfurther");
      		httpPost.setEntity(new UrlEncodedFormEntity(nvps));
      		HttpResponse response2 = httpclient.execute(httpPost);
      
      		try {
      		    System.out.println(response2.getStatusLine());
      		    HttpEntity entity2 = response2.getEntity();
      		    // do something useful with the response body
      		    // and ensure it is fully consumed
      		    EntityUtils.consume(entity2);
      		} finally {
      		    httpPost.releaseConnection();
      		}
      }

      all help appreciated...thanks.

      Comment

      Working...
      X