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;
- public class Main {
- public static void main(String[] args) {
- // create an instance of HttpClient
- HttpClient client = HttpClient.newHttpClient();
- // build request using the Request Builder
- HttpRequest request = HttpRequest.newBuilder()
- .uri(URI.create("http://httpbin.io/user-agent"))
- .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
- .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