How to programmatically get a public IP address?

和自甴很熟 提交于 2020-01-24 10:02:25

问题


I didn't find the right solution. The below code gives me local IP address (if I connected to Wifi, it gives IP address like 192.168.0.x), but I want public IP address (same as if I search in google " what is my IP ")

public static String getLocalIpAddress() {
try {
    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
        NetworkInterface intf = en.nextElement();
        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
            InetAddress inetAddress = enumIpAddr.nextElement();
            if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                return inetAddress.getHostAddress();
            }
        }
    }
} catch (SocketException ex) {
    ex.printStackTrace();
}
return null;
}

OR

WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
    String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());

Can anyone help? Thanks!


回答1:


Step #1: Create a Web service that returns the requester's IP address

Step #2: Call that Web service from your app.

A device does not know its public IP address (unless that device was seriously misconfigured).




回答2:


You may use the WS https://api.whatismyip.com/ip.php from whatismyip.com : This would output only your IP address in the simple text. (No input required, output is optional)

You must be a Gold Level Member to access the API

Updated Answer

You can make use of the web service from ipify.org

Read through the documentation here

Use https://api.ipify.org/?format=json WS to get device public IP address. This would output your IP address in JSON format.

You should use ipify because:

  • You can use it without limit (even if you're doing millions of requests per minute).
  • It's always online and available, and its infrastructure is powered by Heroku, which means that regardless of whether the server running the API dies, or if there's an enormous tornado which destroys half of the east coast, ipify will still be running!
  • It works flawlessly with both IPv4 and IPv6 addresses, so no matter what sort of technology you're using, there won't be issues.

....................

....................




回答3:


I found this simple solution:

public String getExternalIpAddress() throws Exception {
    URL whatismyip = new URL("http://checkip.amazonaws.com");
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(
                whatismyip.openStream()));
        String ip = in.readLine();
        return ip;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Remember that this must be run on a separate thread.




回答4:


Make a call to a server like https://whatismyipaddress.com or http://howtofindmyipaddress.com/.

If you have the page source then parse the ip address out.

There are other servers who only return your ip address. Not a whole html page as above two. But i forgot which one...



来源:https://stackoverflow.com/questions/47812879/how-to-programmatically-get-a-public-ip-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!