全部产品
云市场

请求示例

更新时间:2019-08-02 10:27:34

API请求调用相关示例

  1. package com.aliyun.test;
  2. import codec.Base64;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.client.ClientProtocolException;
  6. import org.apache.http.client.HttpClient;
  7. import org.apache.http.client.methods.HttpGet;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.impl.client.HttpClientBuilder;
  10. import org.apache.http.util.EntityUtils;
  11. import org.junit.Test;
  12. import org.springframework.http.HttpMethod;
  13. import javax.crypto.Mac;
  14. import javax.crypto.SecretKey;
  15. import javax.crypto.spec.SecretKeySpec;
  16. import java.io.IOException;
  17. import java.io.UnsupportedEncodingException;
  18. import java.net.URLEncoder;
  19. import java.text.DateFormat;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Calendar;
  22. import java.util.Map;
  23. import java.util.TreeMap;
  24. import java.util.UUID;
  25. public class SampleAPI {
  26. /**
  27. * https://dm.aliyuncs.com/?Action=SingleSendMail
  28. * &AccountName=test@example.com
  29. * &ReplyToAddress=true
  30. * &AddressType=1
  31. * &ToAddress=test1@example.com
  32. * &Subject=Subject
  33. * &HtmlBody=body
  34. * &<公共请求参数>
  35. * <p>
  36. */
  37. @Test
  38. public void testSingleSendMail() {
  39. Map<String, Object> params = new TreeMap<String, Object>();
  40. params.put("AccessKeyId", AccessKeyId);
  41. params.put("Action", "SingleSendMail");
  42. params.put("Format", Format);
  43. params.put("RegionId", RegionId);
  44. params.put("SignatureMethod", SignatureMethod);
  45. params.put("SignatureNonce", UUID.randomUUID().toString());
  46. params.put("SignatureVersion", SignatureVersion);
  47. params.put("Timestamp", getUTCTimeStr());
  48. params.put("Version", Version);
  49. params.put("AccountName", AccountName);
  50. params.put("AddressType", AddressType);
  51. params.put("HtmlBody", HtmlBody);
  52. params.put("ReplyToAddress", ReplyToAddress);
  53. params.put("Subject", Subject);
  54. params.put("TagName", TagName);
  55. params.put("ToAddress", ToAddress);
  56. Long start = System.currentTimeMillis();
  57. httpRequestSendEmail(params);
  58. System.out.println("耗时 : " + (System.currentTimeMillis() - start));
  59. }
  60. public String httpRequestSendEmail(Map<String, Object> params) {
  61. String result = null;
  62. try {
  63. params.put("Signature", getSignature(prepareParamStrURLEncoder(params), method));
  64. String param = prepareParamStrURLEncoder(params);
  65. String url = protocol + "://" + host + "/?" + param;
  66. System.out.println("-----url-----" + url);
  67. HttpClient httpClient = HttpClientBuilder.create().build();
  68. HttpResponse response = null;
  69. if (method.equals(HttpMethod.GET)) {
  70. HttpGet request = new HttpGet(url);
  71. response = httpClient.execute(request);
  72. } else {
  73. HttpPost request = new HttpPost(url);
  74. response = httpClient.execute(request);
  75. }
  76. System.out.println(response);
  77. if (null != response){
  78. result = EntityUtils.toString(response.getEntity());
  79. }
  80. } catch (ClientProtocolException e) {
  81. e.printStackTrace();
  82. } catch (IOException e) {
  83. e.printStackTrace();
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. }
  87. System.out.println("-----httpRequestSendEmail result-----" + result);
  88. return result;
  89. }
  90. private final static String protocol = "https";
  91. //dm.ap-southeast-1.aliyuncs.com//dm.ap-southeast-2.aliyuncs.com
  92. private final static String host = "dm.aliyuncs.com";
  93. private final static String AccessKeyId = "*******";
  94. private final static String AccessKeySecret = "************";
  95. //发信地址
  96. private final static String AccountName = "noreply@***.com";
  97. //收信地址
  98. private final static String ToAddress = "noreply_test@***.com";
  99. private final static String Format = "JSON";
  100. private final static String SignatureMethod = "HMAC-SHA1";
  101. private final static String SignatureVersion = "1.0";
  102. //2017-06-22
  103. private final static String Version = "2015-11-23";
  104. private final static String AddressType = "1";
  105. //ap-southeast-1//ap-southeast-2
  106. private final static String RegionId = "cn-hangzhou";
  107. private final static Boolean ReplyToAddress = Boolean.TRUE;
  108. private final static String HtmlBody = "<html><body><img alt=\"\" src=\"https://goss4.vcg.com/creative/vcg/400/new/VCG211173951082.jpg\" ><h3>Test send to email ()!</h3></body></html> <a%b' + * %7E> 测试邮件正文。你此次申请注册的验证码为:123456";
  109. private final static String Subject = "测试主题";
  110. private final static String TagName = "测试Tag";
  111. private final static HttpMethod method = HttpMethod.POST;
  112. public String prepareParamStrURLEncoder(Map<String, Object> params) {
  113. try {
  114. StringBuffer param = new StringBuffer();
  115. for (Map.Entry<String, Object> entry : params.entrySet()) {
  116. if (StringUtils.isBlank(entry.getKey()) || null == entry.getValue()) {
  117. continue;
  118. }
  119. param.append(getUtf8Encoder(entry.getKey()) + "=" + getUtf8Encoder(entry.getValue().toString()) + "&");
  120. }
  121. return param.substring(0, param.lastIndexOf("&"));
  122. } catch (Exception e) {
  123. e.printStackTrace();
  124. }
  125. return null;
  126. }
  127. /**
  128. * 获取签名
  129. *
  130. * @param param
  131. * @param method
  132. * @return
  133. * @throws Exception
  134. */
  135. private String getSignature(String param, HttpMethod method) throws Exception {
  136. String toSign = method + "&" + URLEncoder.encode("/", "utf8") + "&"
  137. + getUtf8Encoder(param);
  138. byte[] bytes = HmacSHA1Encrypt(toSign, AccessKeySecret + "&");
  139. return Base64.encode(bytes);
  140. }
  141. private String getUtf8Encoder(String param) throws UnsupportedEncodingException {
  142. return URLEncoder.encode(param, "utf8")
  143. .replaceAll("\\+", "%20")
  144. .replaceAll("\\*", "%2A")
  145. .replaceAll("%7E", "~");
  146. }
  147. private static final String MAC_NAME = "HmacSHA1";
  148. private static final String ENCODING = "UTF-8";
  149. /**
  150. * 使用 HMAC-SHA1 签名方法对对encryptText进行签名
  151. *
  152. * @param encryptText 被签名的字符串
  153. * @param encryptKey 密钥
  154. * @return
  155. * @throws Exception
  156. */
  157. public static byte[] HmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception {
  158. byte[] data = encryptKey.getBytes(ENCODING);
  159. //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
  160. SecretKey secretKey = new SecretKeySpec(data, MAC_NAME);
  161. //生成一个指定 Mac 算法 的 Mac 对象
  162. Mac mac = Mac.getInstance(MAC_NAME);
  163. //用给定密钥初始化 Mac 对象
  164. mac.init(secretKey);
  165. byte[] text = encryptText.getBytes(ENCODING);
  166. //完成 Mac 操作
  167. return mac.doFinal(text);
  168. }
  169. private static DateFormat daetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  170. /**
  171. * 得到UTC时间,类型为字符串,格式为"yyyy-MM-dd HH:mm"<br />
  172. * 如果获取失败,返回null
  173. *
  174. * @return
  175. */
  176. public static String getUTCTimeStr() {
  177. // 1、取得本地时间:
  178. Calendar cal = Calendar.getInstance();
  179. // 2、取得时间偏移量:
  180. int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
  181. // 3、取得夏令时差:
  182. int dstOffset = cal.get(Calendar.DST_OFFSET);
  183. // 4、从本地时间里扣除这些差量,即可以取得UTC时间:
  184. cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
  185. String date = daetFormat.format(cal.getTime());
  186. System.out.println("时间------" + date);
  187. String[] strs = date.split(" ");
  188. return strs[0] + "T" + strs[1] + "Z";
  189. }
  190. }


温馨提示

  1. 杭州:
  2. host = "dm.aliyuncs.com"
  3. AccountName = "填写杭州地区对应的发信地址";
  4. Version = "2015-11-23";
  5. RegionId = "cn-hangzhou";
  6. 新加坡:
  7. host = "dm.ap-southeast-1.aliyuncs.com"
  8. AccountName = "填写新加坡地区对应的发信地址";
  9. Version = "2017-06-22";
  10. RegionId = "ap-southeast-1";
  11. 澳洲:
  12. host = "dm.ap-southeast-2.aliyuncs.com"
  13. AccountName = "填写澳洲地区对应的发信地址";
  14. Version = "2017-06-22";
  15. RegionId = "ap-southeast-2";


——-杭州服务区域最后发出的url请求示例如下——-
https://dm.aliyuncs.com/?AccessKeyId=xxxxxxx&AccountName=noreply%40xxxxxxxx.club&Action=SingleSendMail&AddressType=1&Format=JSON&HtmlBody=%3Chtml%3E%3Cbody%3E%3Cimg%20alt%3D%22Go%E8%AF%AD%E8%A8%80%E4%B8%AD%E6%96%87%E7%BD%91%22%20src%3D%22https%3A%2F%2Fstatic.studygolang.com%2Fimg%2Flogo1.png%22%20%3E%3Cimg%20alt%3D%22%22%20src%3D%22https%3A%2F%2Fgoss4.vcg.com%2Fcreative%2Fvcg%2F400%2Fnew%2FVCG211173951082.jpg%22%20%3E%3Ch3%3ETest%20send%20to%20email%20%EF%BC%88%EF%BC%89%EF%BC%81%3C%2Fh3%3E%3C%2Fbody%3E%3C%2Fhtml%3E%20%3Ca%25b%27%20%2B%20%2A%20%257E%3E%20%E6%B5%8B%E8%AF%95%E9%82%AE%E4%BB%B6%E6%AD%A3%E6%96%87%E3%80%82%E4%BD%A0%E6%AD%A4%E6%AC%A1%E7%94%B3%E8%AF%B7%E6%B3%A8%E5%86%8C%E7%9A%84%E9%AA%8C%E8%AF%81%E7%A0%81%E4%B8%BA%EF%BC%9A123456&RegionId=cn-hangzhou&ReplyToAddress=true&Signature=1PysI0HsZeOEHqQ%3D&SignatureMethod=HMAC-SHA1&SignatureNonce=37161234-7747-419f-b433-bc4719504b01&SignatureVersion=1.0&Subject=%E6%B5%8B%E8%AF%95%E4%B8%BB%E9%A2%98&TagName=%E6%B5%8B%E8%AF%95Tag&Timestamp=2018-11-07T09%3A40%3A13Z&ToAddress=xxxxx%40xxx.com&Version=2015-11-23