Skip to content

Commit d12f94f

Browse files
committed
update InternetChecker to use http connections
1 parent 2b9b98f commit d12f94f

1 file changed

Lines changed: 22 additions & 14 deletions

File tree

src/main/java/de/rwth/idsg/steve/utils/InternetChecker.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package de.rwth.idsg.steve.utils;
22

3-
import java.net.InetSocketAddress;
4-
import java.net.Socket;
3+
import java.net.HttpURLConnection;
4+
import java.net.MalformedURLException;
5+
import java.net.URL;
56
import java.util.Arrays;
67
import java.util.List;
78

@@ -14,16 +15,14 @@
1415
public final class InternetChecker {
1516
private InternetChecker() { }
1617

17-
private static final int PORT = 80;
18-
1918
private static final int CONNECT_TIMEOUT = 5_000;
2019

2120
private static final List<String> HOST_LIST = Arrays.asList(
22-
"github.com",
23-
"google.com",
24-
"facebook.com",
25-
"amazon.com",
26-
"apple.com"
21+
"https://github.com",
22+
"https://www.wikipedia.org",
23+
"https://www.google.com",
24+
"https://www.apple.com",
25+
"https://www.facebook.com"
2726
);
2827

2928
/**
@@ -41,12 +40,21 @@ public static boolean isInternetAvailable() {
4140
return false;
4241
}
4342

44-
private static boolean isHostAvailable(String host) {
45-
try (Socket socket = new Socket()) {
46-
socket.connect(new InetSocketAddress(host, PORT), CONNECT_TIMEOUT);
47-
if (socket.isConnected()) {
48-
return true;
43+
private static boolean isHostAvailable(String str) {
44+
try {
45+
URL url = new URL(str);
46+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
47+
try {
48+
con.setConnectTimeout(CONNECT_TIMEOUT);
49+
con.connect();
50+
if (con.getResponseCode() == 200) {
51+
return true;
52+
}
53+
} finally {
54+
con.disconnect();
4955
}
56+
} catch (MalformedURLException e) {
57+
throw new RuntimeException(e);
5058
} catch (Exception e) {
5159
// No-op
5260
}

0 commit comments

Comments
 (0)