- Apache HttpClient Tutorial
Apache HttpClient - Http Get Request - The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other ef. Apache HttpClient 1 The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed, collaborative, hypermedia information systems. This is the foundation for data communication for the World Wide Web (i.e., Internet) since 1990.
- Apache HttpClient Resources
- Selected Reading
The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.
The HttpClient API provides a class named HttpGet which represents the get request method.
Follow the steps given below to send a get request using HttpClient library
Step 1 - Create a HttpClient object
The createDefault() method of the HttpClients class returns a CloseableHttpClient object, which is the base implementation of the HttpClient interface.
Using this method, create an HttpClient object as shown below −
Step 2 - Create an HttpGet Object
The HttpGet class represents the HTTPGET request which retrieves the information of the given server using a URI.
Create a HTTP GET request by instantiating this class. The constructor of this class acceptsa String value representing the URI.
Step 3 - Execute the Get Request
The execute() method of the CloseableHttpClient class accepts a HttpUriRequest(interface) object (i.e. HttpGet, HttpPost, HttpPut, HttpHead etc.) and returns a responseobject.
Execute the request using this method as shown below −
Example
Following is an example which demonstrates the execution of the HTTP GET request using HttpClient library.
Apache Httpclient Httpresponse
Output
Apache Httpclient Httppost Setheader
The above program generates the following output −