Welcome to our comprehensive guide on using the Java 11 HTTP Client! In this lesson, we'll walk you through creating, sending, and receiving HTTP requests and responses using the new and improved Java 11 HTTP Client. By the end of this tutorial, you'll be able to make real-world applications that interact with APIs.
The Java 11 HTTP Client is an easy-to-use, modern, and powerful API for handling HTTP requests and responses. It replaces the old HttpURLConnection and simplifies the process of making network requests. This tutorial is designed for both beginners and intermediates, so let's get started!
To follow along with this tutorial, you'll need:
Let's start by understanding the basic components of the Java 11 HTTP Client:
Now, let's make our first HTTP request using the Java 11 HTTP Client. We'll send a GET request to the JSONPlaceholder API to fetch a random quote.
import java.net.http.*;
import java.util.concurrent.CompletableFuture;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://jsonplaceholder.typicode.com/quotes/random"))
.build();
CompletableFuture<HttpResponse<String>> responseFuture = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> response = responseFuture.get();
System.out.println(response.body());
}
}In this example, we create an HttpClient instance, build an HttpRequest, and send it using the sendAsync() method. Finally, we print the response body.
What does the `HttpResponse<String>` class represent in the Java 11 HTTP Client?
Now that we've made our first request, let's extract the data we need from the response.
import org.json.JSONObject;
...
HttpResponse<String> response = responseFuture.get();
JSONObject jsonResponse = new JSONObject(response.body());
String quote = jsonResponse.getString("title");
System.out.println(quote);Here, we use the JSONObject class from the org.json library to parse the JSON response and extract the quote's title.
In addition to GET requests, we can also send POST requests with a body using the Java 11 HTTP Client. Let's send a simple POST request to create a new user in our API.
import java.net.http.*;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://jsonplaceholder.typicode.com/users"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"name\": \"John\", \"username\": \"john_doe\", \"email\": \"john_doe@example.com\"}"))
.build();
CompletableFuture<HttpResponse<String>> responseFuture = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> response = responseFuture.get();
System.out.println(response.body());
}
}In this example, we set the request method to POST and provide a JSON string as the request body using the HttpRequest.BodyPublishers.ofString() method.
Congratulations! You've learned the basics of using the Java 11 HTTP Client to make HTTP requests and handle responses. You've also learned how to extract data from JSON responses and send requests with a body. With this knowledge, you can start building powerful applications that interact with APIs.
Keep practicing and exploring the Java 11 HTTP Client to master its features and capabilities. Happy coding! 💡
I hope you found this tutorial helpful! If you have any questions or need further clarification, feel free to ask in the comments section below. Stay tuned for more Java tutorials here at CodeYourCraft! 🚀