Advertisement
dereksir

Untitled

Apr 5th, 2024 (edited)
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. package com.example;
  2.  
  3. // import the required classes
  4. import java.net.URI;
  5. import java.net.http.HttpClient;
  6. import java.net.http.HttpRequest;
  7. import java.net.http.HttpResponse;
  8. import java.net.http.HttpResponse.BodyHandlers;
  9.  
  10. import java.util.List;
  11. import java.util.Random;
  12.  
  13. public class Main {
  14.     public static void main(String[] args) {
  15.         // define a list of User-Agent strings
  16.         List<String> userAgents = List.of(
  17.             "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
  18.             "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
  19.             "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
  20.             // add more User-Agent strings as needed
  21.         );
  22.  
  23.         // randomly select UA from the list
  24.         Random random = new Random();
  25.         String randomUserAgent = userAgents.get(random.nextInt(userAgents.size()));
  26.  
  27.         // create an instance of HttpClient
  28.         HttpClient client = HttpClient.newHttpClient();
  29.  
  30.         // build an HTTP request with a randomly selected User-Agent header
  31.         HttpRequest request = HttpRequest.newBuilder()
  32.             .uri(URI.create("https://httpbin.io/user-agent"))
  33.             .header("User-Agent", randomUserAgent) // Set a random User-Agent header
  34.             .build();
  35.        
  36.         // send request asynchronously and print response to the console
  37.         client.sendAsync(request, BodyHandlers.ofString())
  38.             .thenApply(HttpResponse::body)
  39.             .thenAccept(System.out::println)
  40.             .join();
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement