日期:2014-05-19 浏览次数:20590 次
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main (String[] args) {
System.out.println(area("http://ipwhois.cnnic.net.cn/ipwhoisresult.php?query_option=ipv4&txtquery=123.123.123.123"));
}
public static String area(String urlAddress){
try {
URL url= new URL(urlAddress);
HttpURLConnection h=(HttpURLConnection)url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(h.getInputStream()));
StringBuilder sb = new StringBuilder();
String s;
while((s = in.readLine()) != null){
sb.append(s);
}
s = sb.toString();
return mat(s, "网络名.*?>\\s+(.*?)<");
} catch (Exception e) {
e.printStackTrace();
}
return "未找到相应匹配";
}
public static String mat(String source, String regex){
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(source);
while(matcher.find()){
return matcher.group(1);
}
return " ";
}
}