中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

中國站
幫助中心 > 通信 > API文檔 > 代碼示例 > Java

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. }
鄂伦春自治旗| 永顺县| 竹北市| 黄梅县| 达拉特旗| 永清县| 澄江县| 卢氏县| 定远县| 宜兰县| 巴彦县| 寻乌县| 霍城县| 南城县| 监利县| 河池市| 海淀区| 莫力| 皮山县| 家居| 汪清县| 乡宁县| 图们市| 大足县| 兴山县| 杨浦区| 密云县| 德庆县| 永顺县| 固阳县| 武定县| 建宁县| 宜川县| 富阳市| 从江县| 珲春市| 白水县| 营口市| 通江县| 平遥县| 邹城市|