15 Mayıs 2023 Pazartesi

HttpComponents CloseableHttpAsyncClient Sınıfı

Giriş
Şu satırı dahil ederiz. Soyut bir sınıftır
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
execute metodu
Örnek
Şöyle yaparız
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;

CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
httpClient.start();

HttpGet request = new HttpGet("https://api.example.com/data");

httpClient.execute(request, new FutureCallback<HttpResponse>() {
  @Override
  public void completed(HttpResponse response) {
    System.out.println("Request completed with status: " + response.getStatusLine());
    // Process the response here
  }

  @Override
  public void failed(Exception ex) {
    System.out.println("Request failed: " + ex.getMessage());
  }

  @Override
  public void cancelled() {
    System.out.println("Request cancelled.");
  }
});

// Do other tasks here while the request is being executed asynchronously

// Wait for the response and clean up
httpClient.close();

Hiç yorum yok:

Yorum Gönder