Java IP Address Get getPublicIP()

Here you can find the source of getPublicIP()

Description

get Public IP

License

Open Source License

Declaration

public static String getPublicIP() throws MalformedURLException, IOException 

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.MalformedURLException;
import java.net.URL;

public class Main {
    public static String getPublicIP() throws MalformedURLException, IOException {
        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();/*from  w w w  .  j a  va2 s  .c  o  m*/
        } catch (IOException ex) {
            url = new URL(service2);
            try (BufferedReader input2 = new BufferedReader(new InputStreamReader(url.openStream()))) {
                ip = input2.readLine();
            } catch (IOException ex2) {
                throw ex2;
            }
        }

        return ip;
    }
}

Related

  1. getLongIp(byte[] buff)
  2. getLongIp(String ipString)
  3. getPublicIP()
  4. getPublicIP()
  5. getPublicIP()
  6. getPublicIP()
  7. getPublicIP()
  8. getPublicIP2()
  9. getPublicIPAddress()