Java实现C段域名IP采集
?
?
?
package net.ltan.bbs.util;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;public class DomainUtil { static Pattern urlMatcher = Pattern.compile("(http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?");// 抓网址 /** * 返回c段Ip list * * @param s * @param e * @return */ public static List<String> createCIp(String s, String e) { String[] sarr = s.split("\\."); String[] earr = e.split("\\."); List<String> ip = new ArrayList<String>(); if (Integer.parseInt(earr[2]) - Integer.parseInt(sarr[2]) > -1) { for (int i = Integer.parseInt(sarr[2]); i < Integer .parseInt(earr[2]) + 1; i++) { for (int j = 1; j < 256; j++) { // ip.add(sarr[0]+"."+sarr[1]+"."+i+"."+j); System.out.println("http://sameip.org/ip/" + sarr[0] + "."+ sarr[1] + "." + i + "." + j); getDomain(SendHttpRequest.sendGetRequest("http://sameip.org/ip/" + sarr[0]+ "." + sarr[1] + "." + i + "." + j)); } } } return ip; } public static void get(){ getDomain(SendHttpRequest.sendGetRequest("http://sameip.org/ip/vip.qq.com")); } public static void getDomain(String sourceCode) { String urlS = "<table cellspacing="1" width="100%">"; if (sourceCode.indexOf(urlS) != -1) { String urlE = "</table>"; sourceCode = sourceCode.substring(sourceCode.indexOf(urlS)+ urlS.length(), sourceCode.indexOf(urlE)); Matcher matcher = urlMatcher.matcher(sourceCode); while(matcher.find()){ System.out.println(matcher.group()); } } } public static void main(String[] args) { List<String> ip = createCIp("113.10.245.23", "113.10.245.23"); for (int i = 0; i < ip.size(); i++) { System.out.println(ip.get(i)); }// get(); }}?
发送HTTP请求采用了apahe的HttpClient:
/** * HttpClient发送GET请求 * * @param host * @return */ public static String sendGetRequest(String host) { HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(host); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); String responseBody = null; try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println(getMethod.getStatusLine()); } responseBody = getMethod.getResponseBodyAsString(); } catch (HttpException e) { System.out.println("请求出错!"); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { getMethod.releaseConnection(); } return responseBody; }?
?