java http request
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://openjdk.java.net/"))
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
4.1
10
// open a connection, the request method is "GET" by default
URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
// you can use one of these methods to get the results
connection.connect();
connection.getResponseCode();
connection.getInputStream();
connection.getOutputStream();
// close the connection
connection.disconnect();
Thank you!
10
0
Are there any code examples left?
New code examples in category Java