import java.io.IOException;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
public class IpCheck {
public String getPublicIP1() {
StringBuffer res = new StringBuffer("");
final String siteAddress = "http://ip.8558.org/";
org.jsoup.nodes.Document doc = null;
Connection con = null;
con = Jsoup.connect(siteAddress).timeout(10000);
try {
doc = con.get();
// 取得所有的tr标签
org.jsoup.select.Elements elsTr = doc.body().select("tr");
if (elsTr.size() >= 3) {
// 取得ip和isp所在的最底层标签
org.jsoup.select.Elements els = elsTr.get(2).select("font");
if (els.size() == 2) {
res.append(els.get(0).text());
res.append("(");
res.append(els.get(1).text());
res.append(")");
}
}
} catch (IOException e) {
e.printStackTrace();
}
return res.toString();
}
public String getPublicIP2(){
StringBuffer res = new StringBuffer("") ;
String siteAddress = "http://20140507.ip138.com/ic.asp" ;
org.jsoup.nodes.Document doc = null ;
Connection con = null ;
con = Jsoup.connect(siteAddress).timeout(10000);
try {
doc = con.get();
org.jsoup.select.Elements els = doc.body().select("center");
//您的IP是:[119.40.37.65] 来自:北京市
String text = els.text() ;
int start = text.indexOf("[");
int end = text.indexOf("]");
res.append(text.substring(start + 1, end));
res.append("(");
int pos = text.lastIndexOf(":");
res.append(text.substring(pos+1));
res.append(")");
} catch (IOException e) {
e.printStackTrace();
}
return res.toString() ;
}
public String testOutIP(){
return 0 == getPublicIP1().length() ? getPublicIP2() : getPublicIP1() ;
}
public static void main(String[] args){
IpCheck ic = new IpCheck();
System.out.println("["+ic.testOutIP()+"]");
System.out.println("["+ic.getPublicIP2()+"]");
}
}
需要用到的Jsoup jar包自行下载。
动态检测出口IP,精确到市级
最新推荐文章于 2025-07-19 13:18:34 发布