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.

Download Code

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.

Download Code

 
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:

Send HTTP POST request in Java