liwgfr

Client

Sep 16th, 2021
1,925
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.IOException;
  2. import java.net.URI;
  3. import java.net.http.HttpClient;
  4. import java.net.http.HttpRequest;
  5. import java.net.http.HttpResponse;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) throws IOException, InterruptedException {
  9.         HttpClient client = HttpClient.newHttpClient();
  10.         HttpRequest request = HttpRequest.newBuilder(URI.create("http://localhost:8080/hello"))
  11.                 .GET()
  12.                 .header("accept", "text/plain") // MIME тип
  13.                 .build();
  14.         HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
  15.         System.out.println(response.headers());
  16.         System.out.println(response.body());
  17.  
  18.         request = HttpRequest.newBuilder(URI.create("http://localhost:8080/hello"))
  19.                 .setHeader("Content-type", "text/plain")
  20.                 .POST(HttpRequest.BodyPublishers.ofString("qweqwe"))
  21.                 .build();
  22.         response = client.send(request, HttpResponse.BodyHandlers.ofString());
  23.         System.out.println(response.headers());
  24.         System.out.println(response.body());
  25.     }
  26. }
  27.  
RAW Paste Data