1HttpClient client = HttpClient.newHttpClient();
2HttpRequest request = HttpRequest.newBuilder()
3 .uri(URI.create("http://openjdk.java.net/"))
4 .build();
5client.sendAsync(request, BodyHandlers.ofString())
6 .thenApply(HttpResponse::body)
7 .thenAccept(System.out::println)
8 .join();
1// open a connection, the request method is "GET" by default
2URL url = new URL("http://example.com");
3HttpURLConnection connection = (HttpURLConnection) url.openConnection();
4
5connection.setRequestMethod("GET");
6connection.setRequestProperty("Content-Type", "application/json");
7
8// you can use one of these methods to get the results
9connection.connect();
10connection.getResponseCode();
11connection.getInputStream();
12connection.getOutputStream();
13
14// close the connection
15connection.disconnect();