Making an HTTP GET Request and Parsing Response in Java
- Category Scripts
- Type Script
- Platform Cross-platform
- Language Java
- Price Free
- Copy 4 759
- Comments 0
Making an HTTP GET Request and Parsing Response in Java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class ApiClient {
public static void main(String[] args) {
// Create a modern standard HttpClient instance
HttpClient client = HttpClient.newHttpClient();
// Build the HTTP GET request with an explicit target URL
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://github.com"))
.header("Accept", "application/json")
.GET()
.build();
try {
// Send request and fetch response body as string
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println("Response Body: " + response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Free snippet — copy & paste!
Copy this snippet for free on Clayi Code. One click — no account required.


There are no comments yet :(