<dependency> <groupId>org.asynchttpclient</groupId> <artifactId>async-http-client</artifactId> <version>2.12.3</version> </dependency> import org.asynchttpclient.AsyncHttpClient; import org.asynchttpclient.DefaultAsyncHttpClient; import org.asynchttpclient.Response; public class HttpClientExample { public static void main(String[] args) throws Exception { try (AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient()) { asyncHttpClient.prepareGet("https://api.example.com/data") .execute() .toCompletableFuture() .thenAccept(response -> handleResponse(response)) .join(); } } private static void handleResponse(Response response) { System.out.println(response.getResponseBody()); } } import org.asynchttpclient.AsyncHttpClient; import org.asynchttpclient.DefaultAsyncHttpClientConfig; import org.asynchttpclient.Response; public class HttpClientConfigurationExample { public static void main(String[] args) throws Exception { DefaultAsyncHttpClientConfig.Builder configBuilder = new DefaultAsyncHttpClientConfig.Builder(); try (AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient(configBuilder.build())) { asyncHttpClient.prepareGet("https://api.example.com/data") .execute() .toCompletableFuture() .thenAccept(response -> handleResponse(response)) .join(); } } private static void handleResponse(Response response) { System.out.println(response.getResponseBody()); } }


上一篇:
下一篇:
切换中文