Send HTTP GET request in Java
This post will discuss how to send HTTP GET request in Java.
1. Using URLConnection/HttpURLConnection
The URLConnection class offers several methods for reading and writing directly to the URL over the network. For HTTP-specific features, consider using its subclass HttpURLConnection.
The following program retrieves an URLConnection object by invoking the openConnection() method on an URL. To get a HttpURLConnection object, simply cast URLConnection instance to a HttpURLConnection. Then get an input stream by calling getInputStream() and create a BufferedReader on the input stream to read from it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; class Main { public static void main(String[] args) throws Exception { URL url = new URL("https://reqres.in/api/users?page=1"); URLConnection connection = url.openConnection(); // `HttpURLConnection` connection = (HttpURLConnection) url.openConnection(); // connection.setRequestMethod("GET"); try (BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream()))) { String line; while ((line = in.readLine()) != null) { System.out.println(line); } } } } |
2. Using Apache HttpClient API
If your project is open to external libraries, consider using Apache HttpClient API for executing HTTP methods like GET, POST, PUT, DELETE, etc.
Here’s a simple example that demonstrates how we can use HttpClient APIs to send HTTP GET request. We suggest referring to official HttpClient Examples to get an in-depth understanding of all features offered by this module.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.net.URI; class Main { public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpUriRequest httpget = RequestBuilder.get() .setUri(new URI("https://reqres.in/api/users")) .addParameter("page", "1") .build(); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println(EntityUtils.toString(response.getEntity())); } finally { response.close(); } } finally { httpclient.close(); } } } |
It is worth noting that, like Apache HttpClient, several other Java libraries are available that facilitate easier HTTP requests from Java code. The coverage of any other third-party library is beyond the scope of this article.
That’s all about making an HTTP GET request in Java.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)