API文档

Java

代码示例需要依赖如下jar包

  1. <dependency>
  2. <groupId>org.json</groupId>
  3. <artifactId>json</artifactId>
  4. <version>20190722</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.httpcomponents</groupId>
  8. <artifactId>httpclient</artifactId>
  9. <version>4.4</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.apache.httpcomponents</groupId>
  13. <artifactId>httpmime</artifactId>
  14. <version>4.4</version>
  15. </dependency>

示例代码:

  1. import org.apache.http.HttpEntity;
  2. import org.apache.http.HttpResponse;
  3. import org.apache.http.NameValuePair;
  4. import org.apache.http.client.config.RequestConfig;
  5. import org.apache.http.client.entity.UrlEncodedFormEntity;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.impl.client.CloseableHttpClient;
  8. import org.apache.http.impl.client.HttpClients;
  9. import org.apache.http.message.BasicNameValuePair;
  10. import org.apache.http.util.EntityUtils;
  11. import org.json.JSONObject;
  12. import javax.crypto.Mac;
  13. import javax.crypto.spec.SecretKeySpec;
  14. import javax.xml.bind.DatatypeConverter;
  15. import java.util.*;
  16. public class SmsDemo {
  17. private final static String accessId = "*****";
  18. private final static String accessSecret = "*****";
  19. public static String sign(String s, String key, String method) throws Exception {
  20. Mac mac = Mac.getInstance(method);
  21. SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), mac.getAlgorithm());
  22. mac.init(secretKeySpec);
  23. byte[] hash = mac.doFinal(s.getBytes("UTF-8"));
  24. return DatatypeConverter.printBase64Binary(hash);
  25. }
  26. public static String getStringToSign(TreeMap<String, Object> params) {
  27. StringBuilder s2s = new StringBuilder();
  28. // 签名时要求对参数进行字典排序,此处用TreeMap保证顺序
  29. for (String k : params.keySet()) {
  30. s2s.append(k).append("=").append(params.get(k).toString()).append("&");
  31. }
  32. return s2s.toString().substring(0, s2s.length() - 1);
  33. }
  34. public static void main(String[] args) throws Exception {
  35. String url = "https://api.yisu.com/sms/sendSms";
  36. TreeMap<String, Object> params = new TreeMap<String, Object>(); // TreeMap可以自动排序
  37. Map<String, Object> vars = new HashMap<String, Object>(); // 模板变量
  38. vars.put("code", 123123);
  39. vars.put("min", 5);
  40. JSONObject varsJson = new JSONObject(vars);
  41. params.put("templateVars", varsJson.toString());
  42. params.put("nonce", new Random().nextInt(java.lang.Integer.MAX_VALUE)); // 生成随机数
  43. params.put("timestamp", System.currentTimeMillis() / 1000); // 时间戳
  44. params.put("templateCode", "100001"); // 模板号码
  45. params.put("accessId", accessId);
  46. params.put("phone", "13800000000"); // 手机号码
  47. params.put("signature", sign(getStringToSign(params), accessSecret, "HmacSHA1")); // 签名
  48. // 将所有参数和签名添加到post请求参数数组里
  49. List<NameValuePair> postparams = new ArrayList<NameValuePair>();
  50. for (String s : params.keySet()) {
  51. postparams.add(new BasicNameValuePair(s, params.get(s).toString()));
  52. }
  53. HttpPost httpPost = new HttpPost(url);
  54. try {
  55. httpPost.setEntity(new UrlEncodedFormEntity(postparams, "utf8"));
  56. CloseableHttpClient httpClient;
  57. RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setSocketTimeout(100000).build();
  58. httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
  59. HttpResponse response = httpClient.execute(httpPost);
  60. HttpEntity entity = response.getEntity();
  61. System.out.println(EntityUtils.toString(entity));
  62. } catch (Exception e) {
  63. System.out.println(e.toString());
  64. } finally {
  65. httpPost.releaseConnection();
  66. }
  67. }
  68. }