-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientExampleGet.java
More file actions
32 lines (28 loc) · 974 Bytes
/
Copy pathHttpClientExampleGet.java
File metadata and controls
32 lines (28 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package Java11;
import java.io.IOException;
import java.net.URL;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class HttpClientExampleGet {
public static void main(String[] args) {
//Http client example for get call using java 11 feature example
System.out.println("HttpClient Example in Java 11");
try {
System.out.println(sendGet());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static int sendGet() throws IOException, Exception {
var client = HttpClient.newHttpClient();
var url = new URL("https://www.w3schools.com/");
var request = HttpRequest.newBuilder()
.GET().uri(url.toURI()).build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response);
return response.statusCode();
}
}