Skip to content

Instantly share code, notes, and snippets.

@timpulver
Created September 24, 2012 15:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timpulver/3776469 to your computer and use it in GitHub Desktop.
Save timpulver/3776469 to your computer and use it in GitHub Desktop.
[Java] URL to IP address lookup [host, url, ip, server, convert, conversion]
import java.net.*;
import java.io.*;
public static class IpFromUrl {
// Returns the IP address of an URL
// i.e. http://www.facebook.com -> 123.456.789.10
public static String getIp( String hostname ) throws IOException {
try {
InetAddress ipaddress = InetAddress.getByName(hostname);
System.out.println("IP address: " + ipaddress.getHostAddress());
return ipaddress.getHostAddress();
}
catch ( UnknownHostException e ){
System.out.println("Could not find IP address for: " + hostname);
throw new IOException("Could not find IP address for: " + hostname);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment