亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

中國站
幫助中心 > 通信 > 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. }
丰都县| 马尔康县| 高台县| 旬邑县| 手游| 大兴区| 天峻县| 镇赉县| 罗田县| 柏乡县| 沙坪坝区| 景德镇市| 灯塔市| 昭平县| 玉树县| 鹤壁市| 忻城县| 延庆县| 漠河县| 三门县| 石渠县| 吴旗县| 额敏县| 黑龙江省| 鲁甸县| 苗栗县| 奇台县| 牡丹江市| 台州市| 新乐市| 彰化县| 古蔺县| 夏津县| 广灵县| 璧山县| 青岛市| 民丰县| 修文县| 汪清县| 上蔡县| 南涧|