Skip to content

Instantly share code, notes, and snippets.

@massimilianochiodi
Created April 15, 2022 08:43
Show Gist options
  • Save massimilianochiodi/122c55244ddb63cbe23b9c2f3ec91d63 to your computer and use it in GitHub Desktop.
Save massimilianochiodi/122c55244ddb63cbe23b9c2f3ec91d63 to your computer and use it in GitHub Desktop.
Verify if internet address is reachable
public boolean AddressReachable(String hostUrl) {
try {
URL url = new URL(hostUrl);
final HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(10 * 1000);
urlc.connect();
// Fix per connessione con certificato
if (urlc.getResponseCode() == 200 || urlc.getResponseCode() == 403) {
return true;
}
} catch (Throwable e) {
e.printStackTrace();
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment