47 import java.util.regex.Pattern;
48 import static java.nio.charset.StandardCharsets.UTF_8;
49
50 /*
51 * THE CONTENTS OF THIS FILE HAVE TO BE IN SYNC WITH THE EXAMPLES USED IN THE
52 * JAVADOC.
53 *
54 * @test
55 * @compile JavadocExamples.java
56 */
57 public class JavadocExamples {
58 HttpRequest request = null;
59 HttpClient client = null;
60 Pattern p = null;
61
62 void fromHttpClientClasslevelDescription() throws Exception {
63 //Synchronous Example
64 HttpClient client = HttpClient.newBuilder()
65 .version(Version.HTTP_1_1)
66 .followRedirects(Redirect.NORMAL)
67 .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)))
68 .authenticator(Authenticator.getDefault())
69 .build();
70 HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
71 System.out.println(response.statusCode());
72 System.out.println(response.body());
73
74 //Asynchronous Example
75 HttpRequest request = HttpRequest.newBuilder()
76 .uri(URI.create("https://foo.com/"))
77 .timeout(Duration.ofMinutes(1))
78 .header("Content-Type", "application/json")
79 .POST(BodyPublishers.ofFile(Paths.get("file.json")))
80 .build();
81 client.sendAsync(request, BodyHandlers.ofString())
82 .thenApply(HttpResponse::body)
83 .thenAccept(System.out::println);
84 }
85
86 void fromHttpRequest() throws Exception {
87 // HttpRequest class-level description
88 HttpClient client = HttpClient.newHttpClient();
89 HttpRequest request = HttpRequest.newBuilder()
90 .uri(URI.create("http://foo.com/"))
91 .build();
92 client.sendAsync(request, BodyHandlers.ofString())
93 .thenApply(HttpResponse::body)
94 .thenAccept(System.out::println)
95 .join();
96
97 // HttpRequest.BodyPublishers class-level description
|
47 import java.util.regex.Pattern;
48 import static java.nio.charset.StandardCharsets.UTF_8;
49
50 /*
51 * THE CONTENTS OF THIS FILE HAVE TO BE IN SYNC WITH THE EXAMPLES USED IN THE
52 * JAVADOC.
53 *
54 * @test
55 * @compile JavadocExamples.java
56 */
57 public class JavadocExamples {
58 HttpRequest request = null;
59 HttpClient client = null;
60 Pattern p = null;
61
62 void fromHttpClientClasslevelDescription() throws Exception {
63 //Synchronous Example
64 HttpClient client = HttpClient.newBuilder()
65 .version(Version.HTTP_1_1)
66 .followRedirects(Redirect.NORMAL)
67 .connectTimeout(Duration.ofSeconds(20))
68 .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)))
69 .authenticator(Authenticator.getDefault())
70 .build();
71 HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
72 System.out.println(response.statusCode());
73 System.out.println(response.body());
74
75 //Asynchronous Example
76 HttpRequest request = HttpRequest.newBuilder()
77 .uri(URI.create("https://foo.com/"))
78 .timeout(Duration.ofMinutes(2))
79 .header("Content-Type", "application/json")
80 .POST(BodyPublishers.ofFile(Paths.get("file.json")))
81 .build();
82 client.sendAsync(request, BodyHandlers.ofString())
83 .thenApply(HttpResponse::body)
84 .thenAccept(System.out::println);
85 }
86
87 void fromHttpRequest() throws Exception {
88 // HttpRequest class-level description
89 HttpClient client = HttpClient.newHttpClient();
90 HttpRequest request = HttpRequest.newBuilder()
91 .uri(URI.create("http://foo.com/"))
92 .build();
93 client.sendAsync(request, BodyHandlers.ofString())
94 .thenApply(HttpResponse::body)
95 .thenAccept(System.out::println)
96 .join();
97
98 // HttpRequest.BodyPublishers class-level description
|