现在网上有好多获取外网IP的方法,其实就是访问一个查询IP的网站,然后截取IP,但是那些地址都过期了,我重新找了一个新的地址,亲测可用。
http://1212.ip138.com/ic.asp
public static String getWebIp() {
try {
URL url = new URL("http://1212.ip138.com/ic.asp");
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String s = "";
StringBuffer sb = new StringBuffer("");
String webContent = "";
while ((s = br.readLine()) != null) {
sb.append(s + "\r\n");
}
br.close();
webContent = sb.toString();
int start = webContent.indexOf("[")+1;
int end = webContent.indexOf("]");
webContent = webContent.substring(start,end);
return webContent;
} catch (Exception e) {
e.printStackTrace();
return "error";
}
}
http://1212.ip138.com/ic.asp
public static String getWebIp() {
try {
URL url = new URL("http://1212.ip138.com/ic.asp");
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String s = "";
StringBuffer sb = new StringBuffer("");
String webContent = "";
while ((s = br.readLine()) != null) {
sb.append(s + "\r\n");
}
br.close();
webContent = sb.toString();
int start = webContent.indexOf("[")+1;
int end = webContent.indexOf("]");
webContent = webContent.substring(start,end);
return webContent;
} catch (Exception e) {
e.printStackTrace();
return "error";
}
}