Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example;
- // import the required classes
- import java.net.URI;
- import java.net.http.HttpClient;
- import java.net.http.HttpRequest;
- import java.net.http.HttpResponse;
- import java.net.http.HttpResponse.BodyHandlers;
- import java.util.List;
- import java.util.Random;
- public class Main {
- public static void main(String[] args) {
- // define a list of User-Agent strings
- List<String> userAgents = List.of(
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36",
- "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
- "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"
- // add more User-Agent strings as needed
- );
- // randomly select UA from the list
- Random random = new Random();
- String randomUserAgent = userAgents.get(random.nextInt(userAgents.size()));
- // 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://www.g2.com/products/visual-studio/reviews"))
- .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