A POST request is used to send data to the server; for example, customer information, file upload, etc., using HTML forms.

The HttpClient API provides a class named HttpPost which represents the POST request.

Follow the steps given below to send a HTTP POST request using HttpClient library.

Step 1 - Create an HttpClient Object

The createDefault() method of the HttpClients class returns an object of the class CloseableHttpClient, which is the base implementation of the HttpClient interface.

Using this method, create an HttpClient object.

  1. CloseableHttpClient httpClient = HttpClients.createDefault();

Step 2 - Create HttpPost Object

The HttpPost class represents the HTTP POST request. This sends required data and retrieves the information of the given server using a URI.

Create this request by instantiating the HttpPost class and pass a string value representing the URI, as a parameter to its constructor.

  1. HttpGet httpGet = new HttpGet("http://www.tutorialspoint.com/");

Step 3 - Execute the Get Request

The execute() method of the CloseableHttpClient object accepts a HttpUriRequest (interface) object (i.e. HttpGet, HttpPost, HttpPut, HttpHead etc.) and returns a response object.

  1. HttpResponse httpResponse = httpclient.execute(httpget);

Example

Following is an example which demonstrates the execution of the HTTP POST request using HttpClient library.

  1. import org.apache.http.HttpResponse;
  2. import org.apache.http.client.methods.HttpPost;
  3. import org.apache.http.impl.client.CloseableHttpClient;
  4. import org.apache.http.impl.client.HttpClients;
  5.  
  6. public class HttpPostExample {
  7.  
  8. public static void main(String args[]) throws Exception{
  9.  
  10. //Creating a HttpClient object
  11. CloseableHttpClient httpclient = HttpClients.createDefault();
  12.  
  13. //Creating a HttpGet object
  14. HttpPost httppost = new HttpPost("https://www.tutorialspoint.com/");
  15.  
  16. //Printing the method used
  17. System.out.println("Request Type: "+httppost.getMethod());
  18.  
  19. //Executing the Get request
  20. HttpResponse httpresponse = httpclient.execute(httppost);
  21.  
  22. Scanner sc = new Scanner(httpresponse.getEntity().getContent());
  23.  
  24. //Printing the status line
  25. System.out.println(httpresponse.getStatusLine());
  26. while(sc.hasNext()) {
  27. System.out.println(sc.nextLine());
  28. }
  29. }
  30. }

Output

The above program generates the following output.

  1. Request Type: POST
  2. <!DOCTYPE html>
  3. <!--[if IE 8]><html class = "ie ie8"> <![endif]-->
  4. <!--[if IE 9]><html class = "ie ie9"> <![endif]-->
  5. <!--[if gt IE 9]><!-->
  6. <html lang = "en-US"> <!--<![endif]-->
  7. <head>
  8. <!-- Basic -->
  9. <meta charset = "utf-8">
  10. <title>Parallax Scrolling, Java Cryptography, YAML, Python Data Science, Java
  11. i18n, GitLab, TestRail, VersionOne, DBUtils, Common CLI, Seaborn, Ansible,
  12. LOLCODE, Current Affairs 2018, Apache Commons Collections</title>
  13. <meta name = "Description" content = "Parallax Scrolling, Java Cryptography, YAML,
  14. Python Data Science, Java i18n, GitLab, TestRail, VersionOne, DBUtils, Common
  15. CLI, Seaborn, Ansible, LOLCODE, Current Affairs 2018, Intellij Idea, Apache
  16. Commons Collections, Java 9, GSON, TestLink, Inter Process Communication (IPC),
  17. Logo, PySpark, Google Tag Manager, Free IFSC Code, SAP Workflow"/>
  18. <meta name = "Keywords" content="Python Data Science, Java i18n, GitLab,
  19. TestRail, VersionOne, DBUtils, Common CLI, Seaborn, Ansible, LOLCODE, Gson,
  20. TestLink, Inter Process Communication (IPC), Logo"/>
  21. <meta http-equiv = "X-UA-Compatible" content = "IE = edge">
  22. <meta name = "viewport" conten t= "width = device-width,initial-scale = 1.0,userscalable = yes">
  23. <link href = "https://cdn.muicss.com/mui-0.9.39/extra/mui-rem.min.css"
  24. rel = "stylesheet" type = "text/css" />
  25. <link rel = "stylesheet" href = "/questions/css/home.css?v = 3" />
  26. <script src = "/questions/js/jquery.min.js"></script>
  27. <script src = "/questions/js/fontawesome.js"></script>
  28. <script src = "https://cdn.muicss.com/mui-0.9.39/js/mui.min.js"></script>
  29. </head>
  30. . . . . . . . . . . . . . . . . . . . . . . . .
  31. . . . . . . . . . . . . . . . . . . . . . . . .
  32. . . . . . . . . . . . . . . . . . . . . . . . .
  33. . . . . . . . . . . . . . . . . . . . . . . . .
  34. </script>
  35. </body>
  36. </html>