<dependencies> <dependency> <groupId>io.activej</groupId> <artifactId>activej-http</artifactId> <version>4.0.0</version> </dependency> </dependencies> import io.activej.eventloop.Eventloop; import io.activej.http.HttpResponse; import io.activej.http.async.AsyncHttpClient; import io.activej.http.async.HttpResult; import io.activej.inject.annotation.Inject; import java.io.IOException; public class HttpExample { @Inject private Eventloop eventloop; public void sendGetRequest() throws IOException { AsyncHttpClient client = AsyncHttpClient.create(eventloop); String url = "https://example.com"; client.request(HttpClient.GET, url) .whenComplete((response, exception) -> { if (exception != null) { System.err.println("Request failed: " + exception.getMessage()); } else { System.out.println("Response code: " + response.getCode()); System.out.println("Response body: " + response.getBodyAsString()); } eventloop.shutdown(); }); eventloop.run(); } public static void main(String[] args) throws IOException { HttpExample example = new HttpExample(); example.sendGetRequest(); } }


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