Results 1 to 3 of 3

Thread: Send HTTP POST request from a spring controller

  1. #1
    Join Date
    Jun 2012
    Posts
    7

    Default 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 at 03:52 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2012
    Location
    San Francisco
    Posts
    2

    Default 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
    "

    Quote 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.

Posting Permissions

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