Application example of the HTTP Client framework in the Java library

The HTTP Client framework is a tool commonly used in the Java class library for HTTP communication with the server.It provides a simple and easy -to -use API that allows developers to easily send HTTP requests and deal with response.Below we will introduce the application example of the HTTP Client framework and provide some Java code examples. The HTTP Client framework can be used in various application scenarios, such as obtaining web content, calling RESTFUL API, uploading files, etc.Here are several common application examples: 1. Send GET request to get web content: 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 { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://example.com")) .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body()); } } The above code uses http client to send a get request to "https://example.com" and print the response content. 2. Send post request call Restful API: import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpHeaders; import java.util.HashMap; import java.util.Map; public class HttpClientExample { public static void main(String[] args) throws Exception { HttpClient client = HttpClient.newHttpClient(); Map<String, String> requestBody = new HashMap<>(); requestBody.put("name", "John"); requestBody.put("age", "30"); String requestBodyJson = toJson(requestBody); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://api.example.com/user")) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(requestBodyJson)) .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body()); } private static String toJson(Map<String, String> map) { // Convert the map to a JSON string } } The above code uses http client to send a post request to "https://api.example.com/user" and pass a JSON format request body. 3. Upload file to the server: import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; 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 { HttpClient client = HttpClient.newHttpClient(); Path filePath = Path.of("/path/to/file.txt"); byte[] fileData = Files.readAllBytes(filePath); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://api.example.com/upload")) .POST(HttpRequest.BodyPublishers.ofByteArray(fileData)) .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body()); } } The above code uses http client to send a post request to "https://api.example.com/upload" and upload a file in the form of byte array. These examples demonstrate the application of the HTTP Client framework in the Java class library. It can simplify the implementation of HTTP communication and provide a more convenient and flexible way to interact with the server.Regardless of whether to obtain a simple webpage content or call the RESTFUL API or upload files, HTTP Client is a powerful tool.