现在要做一个程序实现,通过给定IP地址,在 http://ipwhois.cnnic.net.cn/ipwhoisresult.php?query_option=ipv4&txtquery=123.123.123.123 这个地址查询IP归属地信息,txtquery这个参数就是待查询的IP地址。只要得到查询结果中网络名就可以了,有熟悉的吗,给个示范代码,正则表达式解决马上结贴

解决方案 »

  1.   

    通过httpclient,设置该ip归属查询的url,把ip地址替换为需要查询的ip地址,然后调用httpclient的方法向该url请求,获得请求结果后分析结果的内容信息,提取出网络名。
    现在新装的本本没有java环境,代码就交给后面的回复人了。
      

  2.   


    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 " ";
    }
    }
      

  3.   

    很好,感谢一楼二楼,按照二楼的办法基本可以解决我的问题,网上看到很多关于htmlparser的方式做的,有点兴趣,正在找资料,目前的情况,一楼30,二楼70,其余100留给后来者,明天还没人htmlparser做好,翻倍,明天结贴
      

  4.   

    今天没事用jsoup写了一个,确实简单,发来大家看看import java.io.IOException;import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.*;public class JsoupTest { public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println(getIpInfo("123.123.123.123"));
    }
    public static String getIpInfo(String ip) {
    Document doc;
    String out = "";
    try {
    doc = Jsoup.connect(
    "http://ipwhois.cnnic.net.cn/ipwhoisresult.php?query_option=ipv4&txtquery="
    + ip.trim()).get();
    String title = doc.title();
    Element table = doc.select("table").first();
    out = table.select("td").get(3).text();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return out;
    }}