Friday 23 October 2015

HTTP Client: Send HTTP POST request

In this post, I am going to explain how to submit a form using HTTP client.
For example, following servlet class handles user registration data.

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RegistrationHandler
 */
@WebServlet("/RegistrationHandler")
public class RegistrationHandler extends HttpServlet {
  private static final long serialVersionUID = 1L;

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String name = request.getParameter("name");

    PrintWriter out = response.getWriter();
    out.println("Hello " + name);

    out.close();
  }

}

Following HTTP Client application submits data to above servlet and get response.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

public class Main {

  public static void main(String args[]) throws ClientProtocolException,
      IOException, URISyntaxException {
    String uri = "http://localhost:8080/sample_app/RegistrationHandler";

    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    formparams.add(new BasicNameValuePair("name", "Hari Krishna Gurram"));
    formparams.add(new BasicNameValuePair("password", "password"));
    formparams.add(new BasicNameValuePair("mailId", "abcdef@abcdef.com"));
    UrlEncodedFormEntity requestBody = new UrlEncodedFormEntity(formparams,
        Consts.UTF_8);

    HttpPost httppost = new HttpPost(uri);
    httppost.setEntity(requestBody);

    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    CloseableHttpResponse response = httpclient.execute(httppost);

    HttpEntity responseBody = response.getEntity();

    if (responseBody != null) {
      InputStream instream = responseBody.getContent();
      BufferedReader br = new BufferedReader(new InputStreamReader(
          instream));

      try {
        String data;
        while ((data = br.readLine()) != null) {
          System.out.println(data);
        }
      } finally {
        instream.close();
      }
    }

    response.close();
  }
}


Output
Hello Hari Krishna Gurram



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment