import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
public class WhatIP {
public static void main(String[] args) {
String ipString = null;
URL ipURL;
try {
ipURL = new URL("http://myip.xname.org//");
BufferedReader in = new BufferedReader(
new InputStreamReader(ipURL.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
ipString = inputLine;
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// otherwise read the local ip
if(ipString == null || ipString.trim().length()== 0){
try {
ipString = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(ipString);
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
public class WhatIP {
public static void main(String[] args) {
String ipString = null;
URL ipURL;
try {
ipURL = new URL("http://myip.xname.org//");
BufferedReader in = new BufferedReader(
new InputStreamReader(ipURL.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
ipString = inputLine;
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// otherwise read the local ip
if(ipString == null || ipString.trim().length()== 0){
try {
ipString = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(ipString);
}
}
No comments:
Post a Comment