Skip to content

Instantly share code, notes, and snippets.

@barmic
Last active January 20, 2023 08:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barmic/a12c5256f735f4748e3a6f511367407e to your computer and use it in GitHub Desktop.
Save barmic/a12c5256f735f4748e3a6f511367407e to your computer and use it in GitHub Desktop.
Healthcheck

Build

javac HealthCheck.java

Run

java HealthCheck
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class HealthCheck {
public static void main(String[] args) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8080/"))
.GET()
.build();
try {
HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
System.out.println("Status : " + response.statusCode());
System.out.println("Headers : " + response.headers());
System.out.println("Body : " + response.body());
System.exit(1);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment