Java HttpURLConnection Example
The Java java.net.HttpURLConnection
class is used to send a HTTP request. Although it is old and not user-friendly, sometimes it is the only option for some legacy projects. In this article, I will show you how to send GET and POST requests to Go Rest
, which is a fake
or an online REST API for testing and prototyping.
Maven Dependencies #
Add the following dependencies for parsing the results, of course you can use other libraries such as Gson
.
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
GET Request #
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
public class HttpURLConnectionGetExample {
public static void main(String[] args) throws Exception {
String endpoint = "https://gorest.co.in/public/v1/users";
URL url = new URL(endpoint);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader responseReader =
new BufferedReader(new InputStreamReader(connection.getInputStream()));
String temp;
StringBuilder response = new StringBuilder();
while ((temp = responseReader.readLine()) != null) {
response.append(temp);
}
responseReader.close();
JSONObject result = new JSONObject(response.toString());
System.out.println(result.get("meta"));
}
}
Output:
{"pagination":{"total":2145,"pages":108,"limit":20,"links":{"next":"https://gorest.co.in/public/v1/users?page=2","current":"https://gorest.co.in/public/v1/users?page=1","previous":null},"page":1}}
POST Request #
Note that you need to get access token first!
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
public class HttpURLConnectionPostExample {
public static void main(String[] args) throws Exception {
String endpoint = "https://gorest.co.in/public/v1/users";
URL url = new URL(endpoint);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
String accessToken = "accessToken";
connection.setRequestProperty("Authorization", "Bearer " + accessToken);
connection.setDoOutput(true);
String json = "{\"name\":\"john123\", \"gender\":\"male\","
+ "\"email\":\"abc@def.com\",\"status\":\"active\"}";
try(OutputStream os = connection.getOutputStream()) {
byte[] input = json.getBytes("utf-8");
os.write(input, 0, input.length);
os.flush();
os.close();
}
BufferedReader responseReader =
new BufferedReader(new InputStreamReader(connection.getInputStream()));
String temp;
StringBuilder response = new StringBuilder();
while ((temp = responseReader.readLine()) != null) {
response.append(temp);
}
responseReader.close();
JSONObject result = new JSONObject(response.toString());
System.out.println(result);
}
}
Output:
{"data":{"gender":"male","name":"john123","id":2772,"email":"abc@def.com","status":"active"},"meta":null}
- Previous: Firefox Console Log Disabled
- Next: Install SonarQube on Mac