Advertisement
dereksir

Untitled

Apr 5th, 2024
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 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.             .build();
  18.         // send request asynchronously and print response to the console
  19.         client.sendAsync(request, BodyHandlers.ofString())
  20.             .thenApply(HttpResponse::body)
  21.             .thenAccept(System.out::println)
  22.             .join();
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement