I need to make HTTP requests using curl java
. Is cURL built into Java, or do I need to install it separately?
If it requires separate installation, how can I set it up and use it within my Java application?
I need to make HTTP requests using curl java
. Is cURL built into Java, or do I need to install it separately?
If it requires separate installation, how can I set it up and use it within my Java application?
If you’re looking for a native Java solution without installing anything, use :
HttpURLConnection:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class CurlExample {
public static void main(String[] args) throws Exception {
URL url = new URL("https://jsonplaceholder.typicode.com/posts/1");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
System.out.println("Response Code: " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("Response: " + response.toString());
}
}
Why?
No external dependencies required.
Works with Java 8+.
Supports GET, POST, and headers.
For Java 11+, HttpClient is the preferred way to send HTTP requests:
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class CurlExample {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://jsonplaceholder.typicode.com/posts/1"))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Response Code: " + response.statusCode());
System.out.println("Response: " + response.body());
}
}
Modern API with better features.
Easier syntax than HttpURLConnection.
Works only in Java 11+.
For more flexibility (e.g., authentication, custom headers, JSON handling), use Apache HttpClient:
<!-- Add this to your Maven pom.xml -->
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2</version>
</dependency>
</dependencies>
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.io.entity.EntityUtils;
public class CurlExample {
public static void main(String[] args) throws Exception {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpGet request = new HttpGet("https://jsonplaceholder.typicode.com/posts/1");
try (CloseableHttpResponse response = client.execute(request)) {
System.out.println("Response Code: " + response.getCode());
System.out.println("Response: " + EntityUtils.toString(response.getEntity()));
}
}
}
}