java native httpclient utils return with Object

To perform an HTTP request with parameters using Java Native HttpClient and return a custom object, you can modify the code as follows:

 
  

import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandlers; import java.net.http.HttpHeaders; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; public class HttpClientUtils { private final HttpClient client; public HttpClientUtils() { this.client = HttpClient.newHttpClient(); } public CustomResponse sendRequestWithParameters(String url, Map parameters) { StringBuilder queryString = new StringBuilder(); for (Map.Entry entry : parameters.entrySet()) { if (queryString.length() > 0) { queryString.append("&"); } queryString.append(entry.getKey()).append("=").append(entry.getValue()); } String fullUrl = url + "?" + queryString.toString(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(fullUrl)) .GET() .build(); try { HttpResponse response = client.send(request, BodyHandlers.ofString()); int statusCode = response.statusCode(); HttpHeaders headers = response.headers(); String responseBody = response.body(); return new CustomResponse(statusCode, headers, responseBody); } catch (InterruptedException | ExecutionException e) { // Handle exception } return null; } public static void main(String[] args) { HttpClientUtils utils = new HttpClientUtils(); // Example parameters Map parameters = new HashMap<>(); parameters.put("param1", "value1"); parameters.put("param2", "value2"); CustomResponse response = utils.sendRequestWithParameters("https://example.com/api", parameters); if (response != null) { int statusCode = response.getStatusCode(); HttpHeaders headers = response.getHeaders(); String responseBody = response.getBody(); System.out.println("Response Status Code: " + statusCode); System.out.println("Response Headers: " + headers); System.out.println("Response Body: " + responseBody); } } } class CustomResponse { private final int statusCode; private final HttpHeaders headers; private final String body; public CustomResponse(int statusCode, HttpHeaders headers, String body) { this.statusCode = statusCode; this.headers = headers; this.body = body; } public int getStatusCode() { return statusCode; } public HttpHeaders getHeaders() { return headers; } public String getBody() { return body; } }

In this example, the sendRequestWithParameters method takes a URL and a map of parameters as input. It builds the query string by iterating over the parameters and appending them to the URL. Then, it creates an HttpRequest with the complete URL and performs a synchronous GET request.

The method retrieves the status code, headers, and response body from the HttpResponse and constructs a CustomResponse object with these values. The CustomResponse class encapsulates the response information.

In the main method, you can specify the URL and parameters you want to send in the request. The sendRequestWithParameters method is called, and the resulting CustomResponse object is obtained. You can then access the status code, headers, and body of the response as needed.

Note: Handle any exceptions appropriately in the catch block of the sendRequestWithParameters method and handle the case when the response is null in the main method if an exception occurs.

你可能感兴趣的:(java,servlet,开发语言)