调用阿里云api获取解析记录列表

接口文档:获取解析记录列表-https://help.aliyun.com/document_detail/29776.html?spm=5176.doc29699.6.614.vXIo2V

直接post代码:

package JunitTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.SimpleTimeZone;
import java.util.UUID;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;

public class getDescribeDomainRecords {

    @Test
    public void test() throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException{
        try{
               URL url = new URL("http://alidns.aliyuncs.com/?"+getDescribeDomainRecords("starkfang.cn"));
               HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
               urlcon.connect();         //获取连接
               InputStream is = urlcon.getInputStream();
               BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
               StringBuffer bs = new StringBuffer();
               String l = null;
               while((l=buffer.readLine())!=null){
                   bs.append(l).append("\n");
               }
               System.out.println(bs.toString());
            }catch(IOException e){

           }
    }

    /**
     * 
     * @param dns 域名
     * @return
     * @throws UnsupportedEncodingException
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     */
    public static String getDescribeDomainRecords(String dns) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException{

        final String HTTP_METHOD = "GET";

        Map parameters = new HashMap();
        // 加入请求参数
        parameters.put("Action", "DescribeDomainRecords");
        parameters.put("DomainName", dns);
        parameters.put("Version", "2015-01-09");
        parameters.put("AccessKeyId", "testId");//输入你的 Access Key ID
        parameters.put("Timestamp", formatIso8601Date(new Date()));
        parameters.put("SignatureMethod", "HMAC-SHA1");
        parameters.put("SignatureVersion", "1.0");
        parameters.put("SignatureNonce", UUID.randomUUID().toString());
        parameters.put("Format", "XML");

        // 对参数进行排序,注意严格区分大小写
        String[] sortedKeys = parameters.keySet().toArray(new String[]{});
        Arrays.sort(sortedKeys);

        final String SEPARATOR = "&";

        // 生成stringToSign字符串
        StringBuilder stringToSign = new StringBuilder();
        stringToSign.append(HTTP_METHOD).append(SEPARATOR);
        stringToSign.append(percentEncode("/")).append(SEPARATOR);

        StringBuilder canonicalizedQueryString = new StringBuilder();
        for(String key : sortedKeys) {
            // 这里注意对key和value进行编码
            canonicalizedQueryString.append("&")
            .append(percentEncode(key)).append("=")
            .append(percentEncode(parameters.get(key)));
        }

        // 这里注意对canonicalizedQueryString进行编码
        stringToSign.append(percentEncode(canonicalizedQueryString.toString().substring(1)));

        String signature = getSignature(stringToSign.toString());

        canonicalizedQueryString.append("&")
       .append("Signature").append("=")
       .append(percentEncode(signature));

        return canonicalizedQueryString.toString();
    }

    private static final String ISO8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";

    private static String formatIso8601Date(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(ISO8601_DATE_FORMAT);
        df.setTimeZone(new SimpleTimeZone(0, "GMT"));
        return df.format(date);
    }


    private static final String ENCODING = "UTF-8";

    private static String percentEncode(String value)
            throws UnsupportedEncodingException{
        return value != null ?
                URLEncoder.encode(value, ENCODING).replace("+", "%20")
                .replace("*", "%2A").replace("%7E", "~")
                : null;
    }


    // 以下是一段计算签名的示例代码
    private static String getSignature(String stringToSign) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException
    {
        final String ALGORITHM = "HmacSHA1";
        final String ENCODING = "UTF-8";
        String key = "bLJ15dkZHfnMEmaKUYRXh7KCjr4vYe&";//注意 key = Access Key Secret +"&"

        Mac mac = Mac.getInstance(ALGORITHM);
        mac.init(new SecretKeySpec(
                 key.getBytes(ENCODING), ALGORITHM));
        byte[] signData = mac.doFinal(
                  stringToSign.getBytes(ENCODING));

        String signature =
                  new String(Base64.encodeBase64(signData));
        return signature;

    }

}

你可能感兴趣的:(Java)