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<String, String> parameters) { StringBuilder queryString = new StringBuilder(); for (Map.Entry<String, String> 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<String> 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<String, String> 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值