Java IP Address Get getPublicIP2()

Here you can find the source of getPublicIP2()

Description

get Public IP

License

Apache License

Declaration

public static String getPublicIP2() throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String getPublicIP2() throws Exception {
        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;/*from   ww  w.  ja va  2 s.c  o  m*/
        }
        // //System.out.println(inputLine);
        String[] strs = inputLine.split(":");
        ip = strs[1].split("<")[0];
        return ip.trim();
    }
}

Related

  1. getPublicIP()
  2. getPublicIP()
  3. getPublicIP()
  4. getPublicIP()
  5. getPublicIP()
  6. getPublicIPAddress()
  7. getPublicIps()
  8. getPublicIpViaHttp()
  9. getStrLocalIP()