Using the HTTP Client in Java is straightforward. Below is an example demonstrating how to perform a GET request.
// Import necessary classes
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class HttpClientExample {
public static void main(String[] args) throws Exception {
// Create an HttpClient
HttpClient client = HttpClient.newHttpClient();
// Build a GET request
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://jsonplaceholder.typicode.com/posts/1")) // Example URL
.build();
// Send the request and get the response
HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());
// Print the response body
System.out.println(response.body());
}
}
`.
- The example code for making the GET request is enclosed within a `` tag with the specified class for syntax highlighting.
- Keywords related to the content are placed in a ``, and a brief description is in a ``.
- The structure follows your instruction to not include an `` title.
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?