Java
    http

    Java HTTP GET: HttpClient

    Use Java HttpClient for GET requests

    0 views
    Updated 11/17/2025

    Ready to test this code?

    Load this example into the app

    Code Example

    Copy and run
    import java.net.http.*; import java.net.*;
    public class Main {
      public static void main(String[] args) throws Exception {
        var client = HttpClient.newHttpClient();
        var req = HttpRequest.newBuilder(URI.create("https://jsonplaceholder.typicode.com/posts/1")).GET().build();
        var resp = client.send(req, HttpResponse.BodyHandlers.ofString());
        System.out.println(resp.statusCode());
        System.out.println(resp.body());
      }
    }

    Overview

    Overview

    Java HttpClient supports modern HTTP features out of the box.

    Best Practices

    • Use timeouts.
    • Validate status codes.
    • Parse JSON with a library like Jackson.

    Related Topics

    java
    http
    httpclient
    api

    Ready to test APIs like a pro?

    Apicurl is a free, powerful API testing tool.