<menu id="u2y6m"></menu>
<tt id="u2y6m"><rt id="u2y6m"></rt></tt>
  • <input id="u2y6m"></input><tt id="u2y6m"><blockquote id="u2y6m"></blockquote></tt>
    <bdo id="u2y6m"><small id="u2y6m"></small></bdo>
  • <blockquote id="u2y6m"><optgroup id="u2y6m"></optgroup></blockquote>
  • 幫助中心 > 通信 > API文檔 > 代碼示例 > Java

    Java

    代碼示例需要依賴如下jar包

    
        org.json
        json
        20190722
    
    
        org.apache.httpcomponents
        httpclient
        4.4
    
    
        org.apache.httpcomponents
        httpmime
        4.4
    

    示例代碼:

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONObject;
    import javax.crypto.Mac;
    import javax.crypto.spec.SecretKeySpec;
    import javax.xml.bind.DatatypeConverter;
    import java.util.*;
    
    public class SmsDemo {
        private final static String accessId        = "*****";
        private final static String accessSecret    = "*****";
    
        public static String sign(String s, String key, String method) throws Exception {
            Mac mac = Mac.getInstance(method);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), mac.getAlgorithm());
            mac.init(secretKeySpec);
            byte[] hash = mac.doFinal(s.getBytes("UTF-8"));
            return DatatypeConverter.printBase64Binary(hash);
        }
    
        public static String getStringToSign(TreeMap params) {
            StringBuilder s2s = new StringBuilder();
            // 簽名時要求對參數進行字典排序,此處用TreeMap保證順序
            for (String k : params.keySet()) {
                s2s.append(k).append("=").append(params.get(k).toString()).append("&");
            }
            return s2s.toString().substring(0, s2s.length() - 1);
        }
    
        public static void main(String[] args) throws Exception {
            String url = "https://api.yisu.com/sms/sendSms";
            TreeMap params = new TreeMap(); // TreeMap可以自動排序
            Map vars = new HashMap(); // 模板變量
            vars.put("code", 123123);
            vars.put("min", 5);
            JSONObject varsJson = new JSONObject(vars);
            params.put("templateVars", varsJson.toString());
            params.put("nonce", new Random().nextInt(java.lang.Integer.MAX_VALUE)); // 生成隨機數
            params.put("timestamp", System.currentTimeMillis() / 1000); // 時間戳
            params.put("templateCode", "100001"); // 模板號碼
            params.put("accessId", accessId);
            params.put("phone", "13800000000");  // 手機號碼
            params.put("signature", sign(getStringToSign(params), accessSecret, "HmacSHA1")); // 簽名
    
            // 將所有參數和簽名添加到post請求參數數組里
            List postparams = new ArrayList();
            for (String s : params.keySet()) {
                postparams.add(new BasicNameValuePair(s, params.get(s).toString()));
            }
    
            HttpPost httpPost = new HttpPost(url);
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(postparams, "utf8"));
                CloseableHttpClient httpClient;
                RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setSocketTimeout(100000).build();
                httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                System.out.println(EntityUtils.toString(entity));
    
            } catch (Exception e) {
                System.out.println(e.toString());
            } finally {
                httpPost.releaseConnection();
            }
        }
    }
    
    免費撥打  400 100 2938 免費撥打 400 100 2938
    24小時售后技術支持 24小時售后技術支持
    返回頂部 返回頂部
    <menu id="u2y6m"></menu>
    <tt id="u2y6m"><rt id="u2y6m"></rt></tt>
  • <input id="u2y6m"></input><tt id="u2y6m"><blockquote id="u2y6m"></blockquote></tt>
    <bdo id="u2y6m"><small id="u2y6m"></small></bdo>
  • <blockquote id="u2y6m"><optgroup id="u2y6m"></optgroup></blockquote>
  • 一本久道久久综合丁香五月_免费视频禁止18以下禁止观看_日本japanese熟睡人妻_成熟闷骚女邻居引诱2