Advertisement
dereksir

Untitled

Apr 5th, 2024 (edited)
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         //...
  4.        
  5.         // create an instance of HttpClient
  6.         HttpClient client = HttpClient.newHttpClient();
  7.  
  8.         // build an HTTP request with a randomly selected User-Agent header
  9.         HttpRequest request = HttpRequest.newBuilder()
  10.             .uri(URI.create("https://httpbin.io/user-agent"))
  11.             .header("User-Agent", randomUserAgent) // set a random User-Agent header
  12.             .build();
  13.        
  14.         // send request asynchronously and print response to the console
  15.         client.sendAsync(request, BodyHandlers.ofString())
  16.             .thenApply(HttpResponse::body)
  17.             .thenAccept(System.out::println)
  18.             .join();
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement