当前位置: 代码迷 >> Java Web开发 >> IP 地址批量查询!多谢,解决就结贴
  详细解决方案

IP 地址批量查询!多谢,解决就结贴

热度:685   发布时间:2016-04-17 13:00:44.0
IP 地址批量查询!!在线等,谢谢,解决就结贴!
手头有5314个IP,大概有美国,台湾,香港的,我想知道他们都是哪里的!http://www.ip138.com/只能一个个的查询,太慢了!

------解决方案--------------------
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;

public class IP {

  public static void main(String[] args) throws HttpException, IOException {  
    Map <String, String> map = new LinkedHashMap <String, String> ();
    map.put( "211.100.26.124 ", " ");
    map.put( "133.13.233.11 ", " ");
    map.put( "209.131.36.158 ", " ");
    map.put( "138.175.248.10 ", " ");
    map.put( "60.12.227.64 ", " ");
    map.put( "72.21.206.80 ", " ");
    map.put( "61.152.173.36 ", " ");
    long t0 = System.currentTimeMillis();
    int i = 0;
    int size = map.size();
    for (Map.Entry <String, String> entry : map.entrySet()) {
      System.out.printf( "[%d/%d] 正在解析 %s ", ++i, size, entry.getKey());
      String url = "http://www.ip138.com/ips8.asp ";
      HttpClient client = new HttpClient();
      PostMethod method = new PostMethod(url);
      method.setParameter( "action ", "2 ");
      method.setParameter( "ip ", entry.getKey());
      client.executeMethod(method);

      String response = method.getResponseBodyAsString();
      response = new String(response.getBytes( "iso-8859-1 "), "gb2312 ");
      response = response.substring(response.indexOf( "本站主数据: "), response.indexOf( " </li> </ul> "));
      map.put(entry.getKey(), response);
      System.out.println( ",解析完成 ");
      method.releaseConnection();
    }
    System.out.printf( "解析 5314 个 IP 可能要花:%.2f 小时%n ", (System.currentTimeMillis() - t0) * 5314 / (1000.0 * map.size()) / 3600);
    
    // 输出
    //for (Map.Entry <String, String> entry : map.entrySet()) {
    //  System.out.println(entry.getKey() + " --> " + entry.getValue());
    //}
    System.out.println( "OK ");
  }
}

以上代码,需要使用 Commons HttpClient 类库
http://commons.apache.org/downloads/download_httpclient.cgi

以及依赖类库:Commons Logging 和 Commons Codec 类库
http://commons.apache.org/downloads/download_logging.cgi
http://commons.apache.org/downloads/download_codec.cgi

测试了一下解析 5314 个 IP 大约要花两个小时左右的时间。
  相关解决方案