- Multipart Upload
- Custom SSL Context
- Multiple Threads
- Cookies Management
- Form-Based Login
- Proxy Authentication
- Using Proxy
- User Authentication
- Interceptors
- Aborting a Request
- Closing Connection
- Response Handlers
- Http Post Request
- Http Get Request
- Environment Setup
- Overview
- Home
Apache HttpClient Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Apache HttpCpent - Http Post Request
A POST request is used to send data to the server; for example, customer information, file upload, etc., using HTML forms.
The HttpCpent API provides a class named HttpPost which represents the POST request.
Follow the steps given below to send a HTTP POST request using HttpCpent pbrary.
Step 1 - Create an HttpCpent Object
The createDefault() method of the HttpCpents class returns an object of the class CloseableHttpCpent, which is the base implementation of the HttpCpent interface.
Using this method, create an HttpCpent object.
CloseableHttpCpent httpCpent = HttpCpents.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.
HttpGet httpGet = new HttpGet("http://www.tutorialspoint.com/");
Step 3 - Execute the Get Request
The execute() method of the CloseableHttpCpent object accepts a HttpUriRequest (interface) object (i.e. HttpGet, HttpPost, HttpPut, HttpHead etc.) and returns a response object.
HttpResponse httpResponse = httpcpent.execute(httpget);
Example
Following is an example which demonstrates the execution of the HTTP POST request using HttpCpent pbrary.
import org.apache.http.HttpResponse; import org.apache.http.cpent.methods.HttpPost; import org.apache.http.impl.cpent.CloseableHttpCpent; import org.apache.http.impl.cpent.HttpCpents; pubpc class HttpPostExample { pubpc static void main(String args[]) throws Exception{ //Creating a HttpCpent object CloseableHttpCpent httpcpent = HttpCpents.createDefault(); //Creating a HttpGet object HttpPost httppost = new HttpPost("https://www.tutorialspoint.com/"); //Printing the method used System.out.println("Request Type: "+httppost.getMethod()); //Executing the Get request HttpResponse httpresponse = httpcpent.execute(httppost); Scanner sc = new Scanner(httpresponse.getEntity().getContent()); //Printing the status pne System.out.println(httpresponse.getStatusLine()); while(sc.hasNext()) { System.out.println(sc.nextLine()); } } }
Output
The above program generates the following output.
Request Type: POST <!DOCTYPE html> <!--[if IE 8]><html class = "ie ie8"> <![endif]--> <!--[if IE 9]><html class = "ie ie9"> <![endif]--> <!--[if gt IE 9]><!--> <html lang = "en-US"> <!--<![endif]--> <head> <!-- Basic --> <meta charset = "utf-8"> <title>Parallax Scrolpng, Java Cryptography, YAML, Python Data Science, Java i18n, GitLab, TestRail, VersionOne, DBUtils, Common CLI, Seaborn, Ansible, LOLCODE, Current Affairs 2018, Apache Commons Collections</title> <meta name = "Description" content = "Parallax Scrolpng, Java Cryptography, YAML, Python Data Science, Java i18n, GitLab, TestRail, VersionOne, DBUtils, Common CLI, Seaborn, Ansible, LOLCODE, Current Affairs 2018, Intelpj Idea, Apache Commons Collections, Java 9, GSON, TestLink, Inter Process Communication (IPC), Logo, PySpark, Google Tag Manager, Free IFSC Code, SAP Workflow"/> <meta name = "Keywords" content="Python Data Science, Java i18n, GitLab, TestRail, VersionOne, DBUtils, Common CLI, Seaborn, Ansible, LOLCODE, Gson, TestLink, Inter Process Communication (IPC), Logo"/> <meta http-equiv = "X-UA-Compatible" content = "IE = edge"> <meta name = "viewport" conten t= "width = device-width,initial-scale = 1.0,userscalable = yes"> <pnk href = "https://cdn.muicss.com/mui-0.9.39/extra/mui-rem.min.css" rel = "stylesheet" type = "text/css" /> <pnk rel = "stylesheet" href = "/questions/css/home.css?v = 3" /> <script src = "/questions/js/jquery.min.js"></script> <script src = "/questions/js/fontawesome.js"></script> <script src = "https://cdn.muicss.com/mui-0.9.39/js/mui.min.js"></script> </head> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . </script> </body> </html>Advertisements