思路:
通过Jsoup请求ip138获取网络IP
代码:
public static void main(String[] args) {
String localIp = IpUtils.getIp();
System.out.println("本地ip:" + localIp);
IpUtils.resetVpnConnection();//vpn更换ip
localIp = IpUtils.getIp();
System.out.println("本地ip:" + localIp);
}
/**
* 获取本机ip
* @return
*/
public static String getIp(){
try {
Element element = Jsoup.connect("http://www.ip38.com/").get()
.select("a[href^=/ip.php?ip]").first();
if (element != null) {
return element.text();
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}