<dependencies> <dependency> <groupId>com.typesafe.play</groupId> <artifactId>play-standalone-ws_2.12</artifactId> <version>2.8.7</version> </dependency> </dependencies> dependencies { implementation 'com.typesafe.play:play-standalone-ws_2.12:2.8.7' } import play.libs.ws.*; import play.libs.ws.ahc.*; public class MyHttpClient { private final WSClient wsClient; public MyHttpClient() { AhcWSClientConfig wsConfig = new AhcWSClientConfigBuilder().build(); AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient(wsConfig); wsClient = new AhcWSClient(asyncHttpClient); } public WSRequest createRequest(String url) { return wsClient.url(url); } public void close() { wsClient.close(); } } import play.libs.ws.*; import play.libs.ws.ahc.*; public class MyHttpClient { // ... public void fetchData(String url) { WSRequest request = createRequest(url); CompletionStage<WSResponse> responseFuture = request.get(); responseFuture.thenAccept(response -> { System.out.println("Response status: " + response.getStatus()); System.out.println("Response body: " + response.getBody()); }); } // ... } public class Main { public static void main(String[] args) { MyHttpClient httpClient = new MyHttpClient(); try { httpClient.fetchData("http://example.com"); } finally { httpClient.close(); } } }


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