<dependency> <groupId>com.typesafe.play</groupId> <artifactId>play-standalone-ws_2.12</artifactId> <version>2.7.0</version> </dependency> import play.libs.ws.*; import play.libs.ws.ahc.*; public class HttpClient { private final WSClient ws; public HttpClient() { this.ws = StandaloneAhcWSClient.create(); } public String get(String url) throws ExecutionException, InterruptedException { WSRequest request = ws.url(url); CompletionStage<WSResponse> responsePromise = request.get(); WSResponse response = responsePromise.toCompletableFuture().get(); return response.getBody(); } public void close() { ws.close(); } } public class Main { public static void main(String[] args) throws ExecutionException, InterruptedException { HttpClient httpClient = new HttpClient(); String response = httpClient.get("https://example.com"); System.out.println(response); httpClient.close(); } }


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