短信接口申请及使用

云片网短信接口

  1. 注册云片账号
  2. 开发者认证(个人/企业),输入相关信息、上传相关证件,提交审核
  3. 一个账号生成唯一的秘钥,可以有多个子账号
  4. 新增短信签名(加在短信的开头,【腾讯】),提交审核
  5. 新增模板(发送验证码、系统通知、活动通知的模板),提交审核
  6. 开始接触API,妥善保管apikey,选择API对应的使用场景,以发送单条短 信为例,URL:https://sms.yunpian.com/v2/sms/single_send.json ,apikey:9b11127a9701975c734b8aee81ee3526,访问方式:POST,接口支持https及http
    7.jar包yunpian-java-sdk.jar、log4j.jargson.jar、commons-logging.jar
    httpclient.jar (必须4.4以上)、httpcore.jar (必须4.4以上)
 public class Send {
     //编码方式
     private static String ENCODING = "UTF-8";
    /* 单条短信发送,智能匹配短信模板
     * @param apikey 进入后台可查看
     * @param text   需要使用已审核通过的模板或者默认模板
     * @param mobile 接收的手机号,仅支持单号码发送
     * @return json格式字符串
     */
    public static String singleSend(String apikey, String text, String mobile) {
        Map<String, String> params = new HashMap<String, String>();//请求参数集合
        ResourceBundle resource=ReadProperties.getPropertiesInfo();
        params.put("apikey", resource.getString("apikey"));
        params.put("text", text);
        params.put("mobile", mobile);
        return post("https://sms.yunpian.com/v2/sms/single_send.json", params);
    }
   /**
     * 获取签名
     * @param apikey
     * @return
     */
    public static String getQianMing(String apikey){
        Map<String, String>  params=new HashMap<String,String>();
        params.put("apikey", apikey);
        params.put("page_num", "1");
        params.put("page_size", "20");
        return post("https://sms.yunpian.com/v2/sign/get.json", params);
    }
   /**
     * 云片网默认方式
     * @param url
     * @param paramsMap
     * @return
     */
    public static String post(String url, Map<String, String> paramsMap) {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
            try {
                HttpPost method = new HttpPost(url);
                if (paramsMap != null) {
                    List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                    for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                        NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
                        paramList.add(pair);
                    }
                    method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
                }
                response = client.execute(method);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    responseText = EntityUtils.toString(entity, ENCODING);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    response.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return responseText;
        }
   /**
     *调用方式
     */
   public static void main(String[] args) {
        int num = (int) Math.round(Math.random()*9000+1);
        String text="【创客空间】您的验证码是"+num;
        System.out.println(text);
        String msg=Send.singleSend(ReadProperties.getPropertiesInfo().getString("apikey"), text,"15872299124");
        System.out.println(msg);
}

调用成功的返回值示例
{
    "code": 0,
    "msg": "发送成功",
    "count": 1, //成功发送的短信计费条数
    "fee": 0.05,    //扣费条数,70个字一条,超出70个字时按每67字一条计
    "unit": "RMB",  // 计费单位
    "mobile": "13200000000", // 发送手机号
    "sid": 3310228982   // 短信ID
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值