用SendCloud发送短信验证码

Posted rage_angel

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用SendCloud发送短信验证码相关的知识,希望对你有一定的参考价值。


String smsKey = "*****";
            String url = "http://xxxxx";
            String vcode = createRandomVcode();
//            System.out.println("验证码:" + vcode);
            // 填充参数
            Map<String, String> params = new HashMap<String, String>();
            params.put("smsUser", "xxx");
            params.put("templateId", "1");
            params.put("phone", “1562566155256”);
            params.put("vars", "\\"code\\":\\"" + vcode + "\\"");
            // 对参数进行排序
            Map<String, String> sortedMap = new TreeMap<String, String>(new Comparator<String>() 
                @Override
                public int compare(String arg0, String arg1) 
                    // 忽略大小写
                    return arg0.compareToIgnoreCase(arg1);
                
            );
            sortedMap.putAll(params);
            // 计算签名
            StringBuilder sb = new StringBuilder();
            sb.append(smsKey).append("&");
            for (String s : sortedMap.keySet()) 
                sb.append(String.format("%s=%s&", s, sortedMap.get(s)));
            
            sb.append(smsKey);
            String sig = DigestUtils.md5Hex(sb.toString());
            // 将所有参数和签名添加到post请求参数数组里
            List<BasicNameValuePair> postparams = new ArrayList<>();
            for (String s : sortedMap.keySet()) 
                postparams.add(new BasicNameValuePair(s, sortedMap.get(s)));
            
            postparams.add(new BasicNameValuePair("signature", sig));
            HttpPost httpPost = new HttpPost(url);
            try 
                httpPost.setEntity(new UrlEncodedFormEntity(postparams, "utf8"));
                DefaultHttpClient httpclient = new DefaultHttpClient();

                httpclient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
                httpclient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 100000);
                HttpResponse response = httpclient.execute(httpPost);

                HttpEntity entity = response.getEntity();
                EntityUtils.consume(entity);
//                System.out.println(EntityUtils.toString(response.getEntity()));
                String str = EntityUtils.toString(response.getEntity());
//                System.out.println(str);
                JSONObject obj = JSONObject.fromObject(str);
                boolean result = obj.getBoolean("result");

             catch (Exception e) 
                viewData.error("获取验证码失败!" + e.toString());
             finally 
                httpPost.releaseConnection();
            
随机生成6位数验证码:

public String createRandomVcode() 
        //验证码
        String vcode = "";
        for (int i = 0; i < 6; i++) 
            vcode = vcode + (int) (Math.random() * 9);
        
        return vcode;
    



以上是关于用SendCloud发送短信验证码的主要内容,如果未能解决你的问题,请参考以下文章

验证码---短信验证码

用JavaScript写一个简单的倒计时,可以应用在发送短信验证码的“59秒后重新发送验证短信”

通过第三方接口发送短信验证码/短信通知

JAVA利用第三方平台发送短信验证码。

tp5用缓存存储手机短信验证码 ,同时实现了避免60秒内重复发送以及验证功能

用sendcloud来发邮件