Java Utililty Methods IP Address Get

List of utility methods to do IP Address Get

Description

The list of methods to do IP Address Get are organized into topic(s).

Method

StringgetPublicIP()
get Public IP
URL url = null;
final String[] websites = new String[] { "http://checkip.amazonaws.com", "http://myexternalip.com/raw",
        "https://wtfismyip.com/text", "https://api.ipify.org/" };
for (String website : websites) {
    try {
        url = new URL(website);
    } catch (MalformedURLException e) {
        continue;
...
StringgetPublicIP()
get Public IP
String raw = downloadString("http://checkip.dyndns.org/");
raw = raw.substring(raw.indexOf(":") + 1).trim();
raw = raw.substring(0, raw.indexOf("<")).trim();
return raw;
StringgetPublicIP()
get Public IP
String ip = "127.0.0.1";
try {
    for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces
            .hasMoreElements();) {
        NetworkInterface iface = ifaces.nextElement();
        for (Enumeration<InetAddress> addresses = iface.getInetAddresses(); addresses.hasMoreElements();) {
            InetAddress address = addresses.nextElement();
            ip = address.getHostAddress();
...
StringgetPublicIP()
get Public IP
try {
    URL awsCheckIpUrl = new URL("http://checkip.amazonaws.com");
    BufferedReader br = new BufferedReader(new InputStreamReader(awsCheckIpUrl.openStream()));
    return br.readLine();
} catch (IOException e) {
    e.printStackTrace();
    return null;
StringgetPublicIP()
get Public IP
String result = "0.0.0.0";
BufferedReader in = null;
String inputLine = "";
Pattern p = Pattern.compile("\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b");
Matcher m = p.matcher("");
try {
    URL ip = new URL("http://www.whatismyip.com/");
    in = new BufferedReader(new InputStreamReader(ip.openStream()));
...
StringgetPublicIP()
get Public IP
String service1 = "http://checkip.amazonaws.com/";
String service2 = "http://bot.whatismyipaddress.com/";
URL url = new URL(service1);
String ip = "";
try (BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()))) {
    ip = input.readLine();
} catch (IOException ex) {
    url = new URL(service2);
...
StringgetPublicIP2()
get Public IP
String inputLine = "";
String read = "";
String ip = "";
URL url = new URL("http://checkip.dyndns.org/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
while ((read = in.readLine()) != null) {
    inputLine += read;
...
StringgetPublicIPAddress()
This function returns the first external IP address encountered
final String IPV4_REGEX = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";
String ipAddr = null;
Enumeration e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
    NetworkInterface n = (NetworkInterface) e.nextElement();
    Enumeration ee = n.getInetAddresses();
    while (ee.hasMoreElements()) {
        InetAddress i = (InetAddress) ee.nextElement();
...
StreamgetPublicIps()
get Public Ips
return Collections.list(NetworkInterface.getNetworkInterfaces()).stream().filter(ni -> {
    try {
        return (!ni.isLoopback() && !ni.isPointToPoint());
    } catch (SocketException e) {
        throw new RuntimeException("Failed to check network interface!", e);
}).flatMap(ni -> Collections.list(ni.getInetAddresses()).stream()).filter(ia -> !ia.isLinkLocalAddress());
StringgetPublicIpViaHttp()
This will retrieve your IP address via an HTTP server.
final String websites[] = { "http://ip.dorkbox.com/", "http://ip.javalauncher.com/",
        "http://checkip.dyndns.com/", "http://checkip.dyn.com/", "http://curlmyip.com/", "http://tnx.nl/ip",
        "http://ipecho.net/plain", "http://icanhazip.com/", "http://ip.appspot.com/", };
for (int i = 0; i < websites.length; i++) {
    try {
        URL autoIP = new URL(websites[i]);
        BufferedReader in = new BufferedReader(new InputStreamReader(autoIP.openStream()));
        String response = in.readLine().trim();
...