Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- //...
- // create an instance of HttpClient
- HttpClient client = HttpClient.newHttpClient();
- // build an HTTP request with a randomly selected User-Agent header
- HttpRequest request = HttpRequest.newBuilder()
- .uri(URI.create("https://httpbin.io/user-agent"))
- .header("User-Agent", randomUserAgent) // set a random User-Agent header
- .build();
- // send request asynchronously and print response to the console
- client.sendAsync(request, BodyHandlers.ofString())
- .thenApply(HttpResponse::body)
- .thenAccept(System.out::println)
- .join();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement