16 Mayıs 2023 Salı

HttpComponents HttpGet Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.apache.http.client.methods.HttpGet;
constructor
Şöyle yaparız. Burada org.apache.http.util.EntityUtils kullanılıyor
void getFlight() throws Exception {
  HttpClient client = HttpClients.createDefault();
  HttpGet request = new HttpGet("https://www.cleartrip.com");
  HttpResponse response = client.execute(request);
  HttpEntity entity = response.getEntity();

  String responseBody = EntityUtils.toString(entity);
  int statusCode = response.getStatusLine().getStatusCode();
  System.out.println(statusCode);
  System.out.println(responseBody);
}
setHeader metodu
Örnek
Şöyle yaparız. Burada try block içinde kullanılıyor.
try(CloseableHttpClient httpClient = HttpClients.createDefault()) {
  HttpGet httpGet = new HttpGet( url.toString() );
  httpGet.setHeader( "Authorization", String.format( "token %s", "<token>" ));
  httpGet.setHeader( "Accept", "application/vnd.github.v3.raw" );

  try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
     HttpEntity entity = httpResponse.getEntity();

    if (entity != null) {
       FileUtils.copyInputStreamToFile( entity.getContent(), file);
    }
  }
}
setHeaders metodu
Örnek
Şöyle yaparız
String sEndpoint = "https://mydomain.com:9090/sample";
Map<String, String> headers = new HashMap<>();
headers.put(HttpHeaders.AUTHORIZATION, BEARER_TOKEN);

String body = null;
HttpGet get = new HttpGet(sEndpoint);
get.setHeaders(headers.entrySet()alo
  .stream()
  .map(entry -> new BasicHeader(entry.getKey(), entry.getValue()))
  .toArray(Header[]::new));
try (CloseableHttpClient httpClient = ...;
     CloseableHttpResponse response = httpClient.execute(get)) {
  body = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
}


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();

HttpComponents HttpAsyncClients Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.apache.http.impl.nio.client.HttpAsyncClients;
Bu sınıf sayesinde CloseableHttpAsyncClient  yaratılır. Bu sınıf ile gelen he şey senkron çalışır. Asenkron çalışmak için bir başka kütüphane olan HttpClients kullanılır

Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpasyncclient</artifactId>
  <version>4.1.4</version>
</dependency>
createDefault metodu
Örnek
Şöyle yaparız
CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();