15.4.3 HttpResponse Class Explained
The HttpResponse
class is a key component of the Java SE 11 HTTP Client API, which provides a standardized way to handle HTTP responses. Understanding this class is crucial for effectively managing and processing HTTP responses in Java applications.
Key Concepts
1. HttpResponse Interface
The HttpResponse
interface represents the response to an HTTP request. It provides methods to access various aspects of the response, such as the status code, headers, and body.
Example
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); int statusCode = response.statusCode(); String body = response.body();
2. Status Code
The status code is a three-digit integer that indicates the result of the HTTP request. Common status codes include 200 (OK), 404 (Not Found), and 500 (Internal Server Error). The statusCode()
method returns the status code of the response.
Example
if (response.statusCode() == 200) { System.out.println("Request successful!"); } else { System.out.println("Request failed with status code: " + response.statusCode()); }
3. Headers
HTTP headers provide additional information about the response. The headers()
method returns an instance of HttpHeaders
, which contains all the headers of the response.
Example
HttpHeaders headers = response.headers(); headers.map().forEach((name, values) -> { System.out.println(name + ": " + values); });
4. Body
The body of the response contains the actual data returned by the server. The body()
method returns the body of the response as an object of the specified type.
Example
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); String body = response.body(); System.out.println("Response body: " + body);
5. BodyHandlers
BodyHandlers are used to specify how the response body should be handled. Java SE 11 provides several built-in BodyHandlers, such as ofString()
, ofByteArray()
, and ofFile()
.
Example
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); HttpResponse<byte[]> responseBytes = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
Examples and Analogies
Think of the HttpResponse
class as a detailed report that you receive after sending a request to a server. This report contains various sections, such as the status code (indicating whether the request was successful), headers (providing additional details), and the body (containing the main content).
For instance, if you are building a web application that fetches data from an API, the HttpResponse
class allows you to inspect the response and handle different scenarios based on the status code and content.
Example
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); if (response.statusCode() == 200) { String jsonData = response.body(); // Process JSON data } else { System.out.println("API request failed with status code: " + response.statusCode()); }
By mastering the HttpResponse
class, you can effectively manage and process HTTP responses in your Java applications, making your code more robust and maintainable.