#JavaInspires
Apache Http Client - Simple GET request example
Here is the example code to execute simple Http GET request using Apache Http Client library.
we used:
Java 8, Eclipse IDE, Maven, Apache Http Client 4.5.7
Project Folder Structure
----------------------------------------
pom.xml
---------------
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>javainspires</groupId> <artifactId>apachehttpclientexamples</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>apachehttpclientexamples</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.7</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.11</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> </dependencies> </project>
SimpleHttpGETRequestDemo.java
--------------------------------------------------------
Run the above class as Java Application
Output:
-------------
--------------------------------------------------------
package javainspires.apachehttpclientexamples; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; /** * * @author javainspires * */ public class SimpleHttpGETRequestDemo { public static void main(String[] args) { try { // Http Get Request // create http client object HttpClient httpClient = HttpClients.createDefault(); // create http GET request HttpGet httpGetRequest = new HttpGet(); // set request URI to the created request object httpGetRequest.setURI(new URI("https://reqres.in/api/users/2")); // execute request using httpclient object HttpResponse httpResponse = httpClient.execute(httpGetRequest); System.out.println("Status Code - " + httpResponse.getStatusLine().toString()); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Run the above class as Java Application
Output:
-------------
1 | Status Code - HTTP/1.1 200 OK |