Yes, you are correct, to check that you can reach the internet you need to test that explicitly. If you need HTTP access you can try connecting to the host you later on want to connect to.
You should however use the connection method that you intend to use later on. HTTP can work but not FTP. So if you need FTP access you should test it.
If you also want to get the external IP you can use this method:
public static InetAddress getExternalIp() throws IOException {
URL url = new URL("http://automation.whatismyip.com/n09230945.asp");
URLConnection connection = url.openConnection();
connection.addRequestProperty("Protocol", "Http/1.1");
connection.addRequestProperty("Connection", "keep-alive");
connection.addRequestProperty("Keep-Alive", "1000");
connection.addRequestProperty("User-Agent", "Web-Agent");
Scanner s = new Scanner(connection.getInputStream());
try {
return InetAddress.getByName(s.nextLine());
} finally {
s.close();
}
}
If you successfully got an IP address back from this method, you can connect via HTTP to that.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…