`

java调用万网域名接口

    博客分类:
  • java
阅读更多

 java调用万网域名接口

1.常量定义

public class DomainConstants {
	public static class CHARARRAY {
		public static String[] digitalArray = new String[] { "0", "1", "2",
				"3", "4", "5", "6", "7", "8", "9" };
		public static String[] letterArray = new String[] { "a", "b", "c", "d",
				"e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
				"q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
	}

	/**
	 * 在万网www.net.cn上目前支持的域名后缀类型
	 */
	public static class SUFFIX {
		public static String DOMAIN_COM = ".com";
		public static String DOMAIN_CN = ".cn";
		public static String DOMAIN_XYZ = ".xyz";
		public static String DOMAIN_WANG = ".wang";
		public static String DOMAIN_NET = ".net";
		public static String DOMAIN_ORG = ".org";
		public static String DOMAIN_TOP = ".top";
		public static String DOMAIN_REN = ".ren";
		public static String DOMAIN_CLUB = ".club";
		public static String DOMAIN_PUB = ".pub";
		public static String DOMAIN_ROCKS = ".rocks";
		public static String DOMAIN_BAND = ".band";
		public static String DOMAIN_MARKET = ".market";
		public static String DOMAIN_SOFTWARE = ".software";
		public static String DOMAIN_SOCIAL = ".social";
		public static String DOMAIN_LAWYER = ".lawyer";
		public static String DOMAIN_ENGINEER = ".engineer";
		public static String DOMAIN_ME = ".me";
		public static String DOMAIN_BIZ = ".biz";
		public static String DOMAIN_COMCN = ".com.cn";
		public static String DOMAIN_NETCN = ".net.cn";
		public static String DOMAIN_ORGCN = ".org.cn";
		public static String DOMAIN_GOVCN = ".gov.cn";
		public static String DOMAIN_NAME = ".name";
		public static String DOMAIN_INFO = ".info";
		public static String DOMAIN_CC = ".cc";
		public static String DOMAIN_TV = ".tv";
		public static String DOMAIN_MOBI = ".mobi";
		public static String DOMAIN_ASIA = ".asia";
		public static String DOMAIN_CO = ".co";
		public static String DOMAIN_TEL = ".tel";
		public static String DOMAIN_SO = ".so";
		public static String DOMAIN_我爱你 = ".我爱你";
		public static String DOMAIN_中国 = ".中国";
		public static String DOMAIN_公司 = ".公司";
		public static String DOMAIN_网络 = ".网络";
		public static String DOMAIN_集团 = ".集团";
	}

	/**
	 * 在万网www.net.cn上目前支持的域名查询地址
	 */
	public static class NETURL{
		//查询域名是否已经被注册
		public static String DOMAIN_CHECK = "http://panda.www.net.cn/cgi-bin/check.cgi";
		//查询域名注册者联系信息
		public static String DOMAIN_WHOIS = "http://whois.www.net.cn/whois/domain/";
	}
}

 

2.进制转换类定义,后面会用到

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class HexTransformatUtil {  
	/**
	 * 将十进制转换为任意进制值
	 * @param digths 转换后的进制最小位上,依次出现的字符值,比如26进制"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	 * @param num 将被转换的十进制值
	 * @param length 转换到指定字符串后,如果不足length长度位,自动补足最小值,比如26进制"ABCDEFGHIJKLMNOPQRSTUVWXYZ"将在最前补"a"
	 * @return
	 */
    public static String hex10ToAnly(String digths, int num,int length){  
        StringBuffer str = new StringBuffer("");  
        int base = digths.trim().length();
        if(0==num){
        	str.append(digths.charAt(0));
        }else{
        	Stack<Character> s = new Stack<Character>();  
            while(num != 0){  
                s.push(digths.charAt(num%base));  
                num/=base;  
            }  
            while(!s.isEmpty()){  
                str.append(s.pop());  
            }
        }
        String prefix = "";
        String suffix = str.toString();
        if(length>suffix.length()){
        	for(int count = 0;count<length-suffix.length();count++){
        		prefix = prefix + digths.charAt(0);
        	}
        }
        return prefix + suffix;  
    }  
    
    /**
	 * 将任意进制转换为十进制值
     * @param digths 转换前的进制最小位上,依次出现的字符值,比如26进制"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     * @param hexValue 转换前的进制字符串值
     * @return
     */
    public static int hexAnlyTo10(String digths, String hexValue){  
    	if(null==hexValue||"".equals(hexValue.trim()))  return 0;
    	int base = digths.trim().length();
    	
    	Map<String,Integer> digthMap = new HashMap<String,Integer>();
    	int count=0;
    	for(char item : digths.trim().toCharArray()){
    		digthMap.put(""+item, count);
    		count++;
    	}
    	String str = new StringBuffer(hexValue.trim()).reverse().toString();
    	int sum = 0;
    	for(int index = 0;index<str.length();index++){
    		sum = new Double(Math.pow(base, index)).intValue() * digthMap.get(""+str.charAt(index)) +sum;
    	}
    	return sum;
    }  
    
    public static void main(String[] args) {
    	//将十进制"0123456789"值55转换为26进制“ABCDEFGHIJKLMNOPQRSTUVWXYZ”对应的值,不需要格式化最后输出字符串
		System.out.println(hex10ToAnly("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 55, 0));
		
		//将26进制“ABCDEFGHIJKLMNOPQRSTUVWXYZ”字符串值“CD”转换为十进制"0123456789"值55对应的值,不需要格式化最后输出字符串
		System.out.println(hexAnlyTo10("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "CD" ));
	}
}  

 

3.域名检查

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springside.modules.utils.xml.XMLElement;

public class DomainSearch {
	static DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	/**
	 * 利用httpclient发送soap请求
	 * @param postUrl
	 * @param parameterList
	 * @return
	 */
	public static String doPost(String postUrl,
			List<NameValuePair> parameterList) {
		String retStr = "";
		// 创建HttpClientBuilder
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		// HttpClient
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		RequestConfig config = RequestConfig.custom()
				.setConnectTimeout(2000)
				.setSocketTimeout(2000)
				.build();

		// 请求地址
		HttpPost httpPost = new HttpPost(postUrl);
		httpPost.setConfig(config);

		UrlEncodedFormEntity entity;
		try {
			if(null==parameterList) parameterList = new ArrayList<NameValuePair>();
			entity = new UrlEncodedFormEntity(parameterList, "UTF-8");
			httpPost.setEntity(entity);
			CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
			// getEntity()
			HttpEntity httpEntity = response.getEntity();
			if (httpEntity != null) {
				// 打印响应内容
				retStr = EntityUtils.toString(httpEntity, "UTF-8");
				//System.out.println("response:" + retStr);
			}
			// 释放资源
			closeableHttpClient.close();
		} catch (Exception e) {
			System.out.println("httpclient exception:"+e.getMessage());
		}
		return retStr;
	}
	
	/**
	 * 域名是否被注册检查
	 * @param domainName 检查的域名,不包含域名后缀
	 * @param domainSuffix 域名后缀
	 */
	public static void checkDomain(String domainName, String domainSuffix) {
		String domain = domainName + domainSuffix;
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("area_domain", domain));
		params.add(new BasicNameValuePair("timeStamp", dateFormat.format(new Date())));
		String xml = doPost(DomainConstants.NETURL.DOMAIN_CHECK, params);
		XMLElement xmlElement = null;
		try {
			xmlElement = XMLElement.fromXML(xml);
		} catch (Exception e) {
			System.out.println("xml parsing exception :"+xml);
		}
		String original = xmlElement.getLeafValue("original");
		if(original!=null){
			/*if(original.split(":")[0].trim().equals("210")){
				System.out.println("【"+domain+"】  域名可以注册");
			}else if(original.split(":")[0].trim().equals("211")){
				System.out.println("【"+domain+"】域名已经注册");
			}else if(original.split(":")[0].trim().equals("212")){
				System.out.println("【"+domain+"】域名参数传输错误");
			}else if(original.split(":")[0].trim().equals("213")){
				System.out.println("【"+domain+"】域名查询超时");
			}*/
			String codeStr = original.split(":")[0].trim();
			if(codeStr.equals("210")){
				System.out.println(codeStr+"【"+domain+"】  域名可以注册  域名可以注册  域名可以注册  域名可以注册  域名可以注册  域名可以注册");
			}else {
				System.out.println(codeStr+"【"+domain+"】  域名已经注册");
			}
		}
	}

	/**
	 * 检查域名所有者
	 * @param domainName
	 * @param domainSuffix
	 */
	public static void whoisDomain(String domainName, String domainSuffix) {
		String requestUrl = DomainConstants.NETURL.DOMAIN_WHOIS + domainName + domainSuffix;
		String str = doPost(requestUrl,null);
		System.out.println("whoisDomain url:" + requestUrl+",response:"+str);
	}

	public static void main(String[] args) {
		//查询域名jd.com域名注册者信息
		whoisDomain("jd", DomainConstants.SUFFIX.DOMAIN_COM);
		
		//遍历所有四位英文小写字母的com域名(aaaa.com~zzzz.com)是否被注册
		int length = 4;
		int maxValue = new Double(Math.pow(26, length)).intValue()-1;
		int mixValue = 0;
		String digths = "abcdefghijklmnopqrstuvwxyz";
		for(int index = mixValue;index<=maxValue;index++){
			String domain = HexTransformatUtil.hex10ToAnly(digths, index, length);
			checkDomain(domain, DomainConstants.SUFFIX.DOMAIN_COM);

			//万网(www.net.cn)对于频繁调用有相应处理,大致是限制频繁调用的IP在随后几小时内不响应
			//所以这里第一不能使用多线程并发调用,第二要控制每次调用间隔时间,这里两次调用间隔为400毫秒
			try {Thread.sleep(400);} catch (InterruptedException e) {}
		}
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics