edit-icon download-icon

Call Illustration

Last Updated: Jun 29, 2017

This example uses the DescribeDomainRecords interface (the following sample code uses Java; for more complete sample programs and examples in other development languages, refer to the Alibaba Cloud Developers Forum.

The corresponding Action is DescribeDomainRecords and the required parameter is DomainName. After all the public request parameters (except Signature) are added, the request URL will be (for ease of reading, this is the URL before URL encoding):

  1. http://dns.aliyuncs.com/?TimeStamp=2015-01-09T11:10:07Z&Format=xml&AccessKeyId=testid&Action=DescribeDomainRecords&SignatureMethod=HMAC-SHA1&DomainName=example.com&SignatureNonce=1324fd0e-e2bb-4bb1-917c-bd6e437f1710&SignatureVersion=1.0&Version=2015-01-09

According to the signature calculation rules, the Canonicalized Query String is constructed first, as shown below:

  1. http://dns.aliyuncs.com/?TimeStamp=2015-01-09T11:10:07Z&Format=xml&AccessKeyId=testid&Action=DescribeDomainRecords&SignatureMethod=HMAC-SHA1&DomainName=example.com&SignatureNonce=1324fd0e-e2bb-4bb1-917c-bd6e437f1710&SignatureVersion=1.0&Version=2015-01-09

Then, StringToSign is constructed as a signature string with a value:

  1. GET&%2F&AccessKeyId%3Dtestid&Action%3DDescribeDomainRecords&Format%3Dxml&DomainName%3Dexample.com&SignatureMethod%3DHMAC-SHA1&SignatureNonce%3D1324fd0e-e2bb-4bb1-917c-bd6e437f1710&SignatureVersion%3D1.0&TimeStamp%3D2015-01-09T11%253A10%253A07Z&Version%3D2015-01-09

The following Java sample code demonstrates how to add public request parameters, how to construct the Canonicalized Query String from the request parameters, and how to construct the StringToSign. This example assumes that all request parameters are placed in a Map<String, String> object and the Access Key ID is “testid”.

  1. final String HTTP_METHOD = "GET";
  2. Map<String, String> parameters = new HashMap<String, String>();
  3. // Add public request parameters
  4. parameters.put("Action", "DescribeDomainRecords");
  5. parameters.put("DomainName", "example.com");
  6. parameters.put("Version", "2015-01-09");
  7. parameters.put("AccessKeyId", "testid");
  8. parameters.put("TimeStamp", formatIso8601Date(new Date()));
  9. parameters.put("SignatureMethod", "HMAC-SHA1");
  10. parameters.put("SignatureVersion", "1.0");
  11. parameters.put("SignatureNonce", UUID.randomUUID().toString());
  12. parameters.put("Format", "XML");
  13. // Sort the parameters, and note they are strictly case-sensitive
  14. String[] sortedKeys = parameters.keySet().toArray(new String[]{});
  15. Arrays.sort(sortedKeys);
  16. final String SEPARATOR = "&";
  17. // Generate the stringToSign
  18. StringBuilder stringToSign = new StringBuilder();
  19. stringToSign.append(HTTP_METHOD).append(SEPARATOR);
  20. stringToSign.append(percentEncode("/")).append(SEPARATOR);
  21. StringBuilder canonicalizedQueryString = new StringBuilder();
  22. for(String key : sortedKeys) {
  23. // Be sure to encode the key and value
  24. canonicalizedQueryString.append("&")
  25. .append(percentEncode(key)).append("=")
  26. .append(percentEncode(parameters.get(key)));
  27. }
  28. // Be sure to encode the canonicalizedQueryString
  29. stringToSign.append(percentEncode(canonicalizedQueryString.toString().substring(1)));

In this example, please note that the TimeStamp parameter must comply with ISO8601 norms. Furthermore, please note that UTC time is used. Otherwise, the request will encounter an error. The following sample code demonstrates how a compliant TimeStamp string is generated:

  1. private static final String ISO8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
  2. private static String formatIso8601Date(Date date) {
  3. SimpleDateFormat df = new SimpleDateFormat(ISO8601_DATE_FORMAT);
  4. df.setTimeZone(new SimpleTimeZone(0, "GMT"));
  5. return df.format(date);
  6. }

When generating a Canonicalized Query String (the canonicalizedQueryString variable in the example) and StringToSign, both must be encoded. The encoding rules are described in detail in the signature mechanism section. The following sample code demonstrates how to use the java.net.URLEncoder class for encoding:

  1. private static final String ENCODING = "UTF-8";
  2. private static String percentEncode(String value)
  3. throws UnsupportedEncodingException{
  4. return value != null ?
  5. URLEncoder.encode(value, ENCODING).replace("+", "%20")
  6. .replace("*", "%2A").replace("%7E", "~")
  7. : null;
  8. }

If we assume the “Access Key ID” is “testid”, the “Access Key Secret” is “testsecret”, and the Key used for HMAC calculation is “testsecret&”, the calculated signature value is:

  1. SmhZuLUnXmqxSEZ/GqyiwGqmf+M=

Sample code for signature calculation (Java):

  1. // The following is a sample code for signature calculation
  2. final String ALGORITHM = "HmacSHA1";
  3. final String ENCODING = "UTF-8";
  4. key = "testsecret&";
  5. Mac mac = Mac.getInstance(ALGORITHM);
  6. mac.init(new SecretKeySpec(
  7. key.getBytes(ENCODING), ALGORITHM));
  8. byte[] signData = mac.doFinal(
  9. stringToSign.getBytes(ENCODING));
  10. String signature =
  11. new String(Base64.encodeBase64(signData));

After adding the Signature parameter, please perform URL encoding according to the RFC3986 rules and the following message appears:

  1. http://dns.aliyuncs.com/?TimeStamp=2015-01-09T11%3A10%3A07Z&Format=xml&AccessKeyId=testid&Action=DescribeDomainRecords&SignatureMethod=HMAC-SHA1&DomainName=example.com&SignatureNonce=1324fd0e-e2bb-4bb1-917c-bd6e437f1710&SignatureVersion=1.0&Version=2015-01-09&Signature=SmhZuLUnXmqxSEZ%2FGqyiwGqmf%2BM%3D

Next, send an HTTP request to the above URL and a response to the request will be received from the DNS server, as shown below:

  1. <DescribeDomainRecordsResponse>
  2. <PageNumber>1</PageNumber>
  3. <DomainRecords>
  4. <Record>
  5. <DomainName>example.com</DomainName>
  6. <Line>default</Line>
  7. <Priority>1</Priority>
  8. <RR>2222</RR>
  9. <RecordId>51542601</RecordId>
  10. <TTL>600</TTL>
  11. <Type>MX</Type>
  12. <Value>hichina.com</Value>
  13. </Record>
  14. <Record>
  15. <DomainName>example.com</DomainName>
  16. <Line>default</Line>
  17. <RR>234234</RR>
  18. <RecordId>51542600</RecordId>
  19. <TTL>600</TTL>
  20. <Type>SRV</Type>
  21. <Value>3 0 2176 example-server.2.google.com</Value>
  22. </Record>
  23. <Record>
  24. <DomainName>example.com</DomainName>
  25. <Line>default</Line>
  26. <RR>2222</RR>
  27. <RecordId>51542068</RecordId>
  28. <TTL>600</TTL>
  29. <Type>A</Type>
  30. <Value>3.4.5.6</Value>
  31. </Record>
  32. </DomainRecords>
  33. <PageSize>3</PageSize>
  34. <TotalCount>3</TotalCount>
  35. <RequestId>F3440766-E1C5-4110-8285-5F53F6750560</RequestId>
  36. </DescribeDomainRecordsResponse>

By parsing this XML result, the user obtains a list of all resolution records under the domain name. If the Format parameter is specified as JSON to submit a request, the result will also be returned in the JSON format.

Thank you! We've received your feedback.