-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatusCode.java
executable file
·41 lines (34 loc) · 1.35 KB
/
StatusCode.java
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
33
34
35
36
37
38
39
40
41
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 11+
//DEPS info.picocli:picocli:4.6.2
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;
import java.io.InputStream;
import java.net.URL;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.Callable;
@Command(name = "StatusCode", mixinStandardHelpOptions = true, version = "StatusCode 0.1", description = "StatusCode made with JBang and Java 11 HttpClient")
class StatusCode implements Callable<Integer> {
@Parameters(arity = "1", index = "0", description = "source URL")
URL url;
public static void main(final String... args) {
new CommandLine(new StatusCode()).execute(args);
System.exit(0);
}
@Override
public Integer call() throws Exception {
final HttpClient client = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();
final HttpRequest request = HttpRequest.newBuilder(url.toURI())
.GET()
.build();
final HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
final int statusCode = response.statusCode();
System.out.println(statusCode);
return statusCode;
}
}