forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHost-checker.java
35 lines (30 loc) · 1.21 KB
/
Host-checker.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
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class WebsiteStatusChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the website URL to check: ");
String urlToCheck = scanner.nextLine();
try {
int statusCode = checkWebsiteStatus(urlToCheck);
if (statusCode >= 200 && statusCode < 400) {
System.out.println("Website is alive (HTTP Status Code: " + statusCode + ")");
} else {
System.out.println("Website is down (HTTP Status Code: " + statusCode + ")");
}
} catch (IOException e) {
System.err.println("Error checking website status: " + e.getMessage());
}
}
public static int checkWebsiteStatus(String url) throws IOException {
URL websiteURL = new URL(url);
HttpURLConnection connection = (HttpURLConnection) websiteURL.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int statusCode = connection.getResponseCode();
connection.disconnect();
return statusCode;
}
}