Advertisement
dereksir

Untitled

Apr 5th, 2024 (edited)
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 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. public class Main {
  11.     public static void main(String[] args) {
  12.         // create an instance of HttpClient
  13.         HttpClient client = HttpClient.newHttpClient();
  14.         // build request using the Request Builder
  15.         HttpRequest request = HttpRequest.newBuilder()
  16.             .uri(URI.create("http://httpbin.io/user-agent"))
  17.             .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36") // set the User-Agent header
  18.             .build();
  19.         // send request asynchronously and print response to the console
  20.         client.sendAsync(request, BodyHandlers.ofString())
  21.             .thenApply(HttpResponse::body)
  22.             .thenAccept(System.out::println)
  23.             .join();
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement