|
| 1 | +packagecom.winterbe.java11; |
| 2 | + |
| 3 | +importjava.io.IOException; |
| 4 | +importjava.net.Authenticator; |
| 5 | +importjava.net.PasswordAuthentication; |
| 6 | +importjava.net.URI; |
| 7 | +importjava.net.http.HttpClient; |
| 8 | +importjava.net.http.HttpRequest; |
| 9 | +importjava.net.http.HttpResponse; |
| 10 | +importjava.time.Duration; |
| 11 | + |
| 12 | +publicclassHttpClientExamples { |
| 13 | + |
| 14 | +publicstaticvoidmain(String[]args)throwsIOException,InterruptedException { |
| 15 | +// syncRequest(); |
| 16 | +// asyncRequest(); |
| 17 | +// postData(); |
| 18 | +basicAuth(); |
| 19 | + } |
| 20 | + |
| 21 | +privatestaticvoidsyncRequest()throwsIOException,InterruptedException { |
| 22 | +varclient =HttpClient.newHttpClient(); |
| 23 | +varrequest =HttpRequest.newBuilder() |
| 24 | + .uri(URI.create("https://winterbe.com")) |
| 25 | + .build(); |
| 26 | +HttpResponse<String>response =client.send(request,HttpResponse.BodyHandlers.ofString()); |
| 27 | +System.out.println(response.body()); |
| 28 | + } |
| 29 | + |
| 30 | +privatestaticvoidasyncRequest() { |
| 31 | +varclient =HttpClient.newHttpClient(); |
| 32 | +varrequest =HttpRequest.newBuilder() |
| 33 | + .uri(URI.create("https://winterbe.com")) |
| 34 | + .build(); |
| 35 | +client.sendAsync(request,HttpResponse.BodyHandlers.ofString()) |
| 36 | + .thenApply(HttpResponse::body) |
| 37 | + .thenAccept(System.out::println); |
| 38 | + } |
| 39 | + |
| 40 | +privatestaticvoidpostData()throwsIOException,InterruptedException { |
| 41 | +varclient =HttpClient.newHttpClient(); |
| 42 | +varrequest =HttpRequest.newBuilder() |
| 43 | + .uri(URI.create("https://postman-echo.com/post")) |
| 44 | + .timeout(Duration.ofSeconds(30)) |
| 45 | + .version(HttpClient.Version.HTTP_2) |
| 46 | + .header("Content-Type","text/plain") |
| 47 | + .POST(HttpRequest.BodyPublishers.ofString("Hi there!")) |
| 48 | + .build(); |
| 49 | +varresponse =client.send(request,HttpResponse.BodyHandlers.ofString()); |
| 50 | +System.out.println(response.statusCode());// 200 |
| 51 | + } |
| 52 | + |
| 53 | +privatestaticvoidbasicAuth()throwsIOException,InterruptedException { |
| 54 | +varclient =HttpClient.newHttpClient(); |
| 55 | +varrequest1 =HttpRequest.newBuilder() |
| 56 | + .uri(URI.create("https://postman-echo.com/basic-auth")) |
| 57 | + .build(); |
| 58 | +varresponse1 =client.send(request1,HttpResponse.BodyHandlers.ofString()); |
| 59 | +System.out.println(response1.statusCode());// 401 |
| 60 | + |
| 61 | +varauthClient =HttpClient |
| 62 | + .newBuilder() |
| 63 | + .authenticator(newAuthenticator() { |
| 64 | +@Override |
| 65 | +protectedPasswordAuthenticationgetPasswordAuthentication() { |
| 66 | +returnnewPasswordAuthentication("postman","password".toCharArray()); |
| 67 | + } |
| 68 | + }) |
| 69 | + .build(); |
| 70 | +varrequest2 =HttpRequest.newBuilder() |
| 71 | + .uri(URI.create("https://postman-echo.com/basic-auth")) |
| 72 | + .build(); |
| 73 | +varresponse2 =authClient.send(request2,HttpResponse.BodyHandlers.ofString()); |
| 74 | +System.out.println(response2.statusCode());// 200 |
| 75 | + } |
| 76 | + |
| 77 | +} |