用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;
    }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值