Java IP Address Get getExternalIPAddress()

Here you can find the source of getExternalIPAddress()

Description

get External IP Address

License

Open Source License

Declaration

public static String getExternalIPAddress() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static String getExternalIPAddress() {
        BufferedReader in = null;
        try {/*  www . j av  a 2s .c om*/
            //         URL whatismyip = new URL("http://myip.dnsomatic.com");
            //         in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
            //         return in.readLine(); //you get the IP as a String

            URL whatismyip = new URL("http://checkip.amazonaws.com");

            URLConnection connection = whatismyip.openConnection();
            connection.addRequestProperty("Protocol", "Http/1.1");
            connection.addRequestProperty("Connection", "keep-alive");
            connection.addRequestProperty("Keep-Alive", "1000");
            connection.addRequestProperty("User-Agent", "Web-Agent");

            in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));

            return in.readLine(); //you get the IP as a String  
        } catch (IOException ex) {
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
            }
        }
        return null;
    }
}

Related

  1. getExternalIp()
  2. getExternalIp()
  3. getExternalIp()
  4. getExternalIP(String host, String regexPattern)
  5. getExternalIPAddress()
  6. getExternalIPAddress()
  7. getFirstLocalNonLoopbackIpAddress()
  8. getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6)
  9. getFirstNonLoopBackAddress(boolean preferIpv4, boolean preferIPv6)