Aşağıdaki örnekte java.net.http.HttpClient ile URL GET methodu ile çağrılmakta ve dönen response ekrana basılmaktadır.
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class GetTest {
public static void main(String[] args)
throws IOException, InterruptedException {
HttpClient httpClient=HttpClient.newHttpClient();
HttpRequest httpRequest=HttpRequest
.newBuilder(URI.create("https://www.google.com"))
.GET().build();
HttpResponse<String>
response = httpClient.send(httpRequest,
HttpResponse.BodyHandlers.ofString());
System.out.println("Durum Kodu: " + response.statusCode());
System.out.println("Gelen Veri: " + response.body());
}
}
Bu kod çalıştırıldığında www.google.com'un ana sayfasının HTML kodları ekrana basılacaktır.