短信发送api示例

来源:互联网 发布:阿里云如何打开80端口 编辑:程序博客网 时间:2024/05/08 07:38

首先说一下短信验证的使用场景。
       1、与账户信息相关的操作,如修改支付宝账号等
       2、与资金流转相关的操作,如提现等
       只有在以上两种场景下,短信验证才会起到作用。即提交相关操作的时候,附带一个验证码。
       在本项目中,使用的短信平台是云片网。本文会举一个例子来演示短信:

流程

1、云片网上的短信是通过一个http请求触发的。

    // 通用发送接口的http地址    private static String URI_SEND_SMS = "http://yunpian.com/v1/sms/send.json";    //模板发送接口的http地址    private static String URI_TPL_SEND_SMS = "https://sms.yunpian.com/v1/sms/tpl_send.json";    //发送语音验证码接口的http地址    private static String URI_SEND_VOICE = "https://voice.yunpian.com/v1/voice/send.json";     /**     * 基于HttpClient 4.3的通用POST方法     *     * @param url     *            提交的URL     * @param paramsMap     *            提交<参数,值>Map     * @return 提交响应     */    public static String post(String url, Map<String, String> paramsMap) {        StringBuilder str=new StringBuilder("调用发送短信外部接口:post-->");        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);            }        } catch (Exception e) {            e.printStackTrace();        } finally {            try {                response.close();            } catch (Exception e) {                e.printStackTrace();            }        }        str.append("responseText:").append(responseText).append("_");        System.out.println("responseText:"+responseText);        return responseText;    }

1、在云片网上注册一个帐号,购买相应的服务。
2、设置发送短信的模版
       模版的组成部分:【签名】+短信正文
       先要申请签名。签名通过之后,在使用该签名添加模版。
       格式如:【XXX】亲爱的用户,您的注册码是#code#。如非本人操作,请忽略此短信。
3、三种不同的方式
       1)发送模版的时候,可以以text文本发送,即发送的时候text与模版进行匹配

    /**     * 使用模版匹配方式发短信     *     * @param apikey    apikey(来自云片网的账户信息)     * @param text   短信内容     * @param mobile   接受的手机号     * @return json格式字符串     * @throws IOException     */    public static String sendSms(String apikey, String text, String mobile)            throws IOException {        Map<String, String> params = new HashMap<String, String>();        //短信验证码——模糊匹配方式        params.put("apikey", apikey);        params.put("text", text);        params.put("mobile", mobile);        return post(URI_SEND_SMS, params);    }

       2)另一种方式是,直接指定模版id进行发送

    /**     * 指定模版发短信     *     * @param apikey    apikey(来自云片网的账户信息)     * @param tpl_id   模版id     * @param tpl_value  模版中的通配符     * @param mobile   接受的手机号     * @return json格式字符串     * @throws IOException     */    public static String sendSms(String apikey, String text, String mobile)            throws IOException {        Map<String, String> params = new HashMap<String, String>();        //短信验证码——使用模版        params.put("apikey", apikey);        params.put("tpl_id", "994567"); //模版id        params.put("tpl_value", "#code#=1234");        params.put("mobile", mobile);        return post(URI_TPL_SEND_SMS, params);    }

       3)使用语音播报的方式

    /**     * 使用语音播报的方式     *     * @param apikey    apikey(来自云片网的账户信息)     * @param code  验证码     * @param mobile   接受的手机号     * @return json格式字符串     * @throws IOException     */    public static String sendSms(String apikey, String text, String mobile)            throws IOException {        Map<String, String> params = new HashMap<String, String>();        //使用语音播报        params.put("apikey", apikey);        params.put("mobile", mobile);        params.put("code", "456789");        return post(URI_SEND_VOICE, params);    }

调用发送短信:

    public static void main(String[] args) throws IOException {        String apikey = "云片网上的apikey"        String text = "【签名】您的验证码是";        String code = "";        Random random = new Random();        for (int i = 0; i < 6; i++) {            code += random.nextInt(10);        }        text = text + code;        sendSms(apikey, text, "187XXXX5771");//手机号        System.out.println("code:" + code);    }

       短信发送,算是一块成熟的技术。所以肯定有第三方程序已经提供了。我们需要做的就是找到这些程序,把他们提供的使用到我们的项目中。

1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 派派背包食物不足怎么办 未转变者下不了怎么办 小升初户籍与房产不一致怎么办 小孩在外地读书怎么办计生证明 泉州居住证要半年小孩读书怎么办 孩子上学有兰山户口没有房产怎么办 培训机构跑路了怎么办 报的培训班跑路怎么办 巡视组举报后会怎么办 巡视组交办不办怎么办 分手以后还要不要联系忘不了怎么办 父母穷且不上进怎么办 惹父母生气了该怎么办 小孩戒奶不吃奶粉怎么办 孩子听不进去话怎么办 异性好朋友喜欢自己亲吻自己怎么办 对方对你反感了怎么办 家长偷看孩子日记老师怎么办 儿子与父母相冲怎么办 初二孩子不争气老师打他怎么办? 孩子被老师打又怎么办 家里2个孩子打架怎么办 一年级的学生特别会顶嘴怎么办 私立学校的学生顶嘴老师该怎么办 孩子做错事家长不道歉怎么办 孩孑语文成绩差怎么办 高三了孩子不愿意补课怎么办 四年级的孩子上课喜欢讲小话怎么办 五年级孩子太叛逆怎么办 二年级话唠孩子怎么办 家有老人带孩子怎么办 不会看孩子。孩子一哭就害怕怎么办 孩子不老实爱动怎么办 4周孩子脾气大怎么办 老公脾气暴躁爱骂人怎么办? 当妈妈了脾气不好怎么办 父亲很坏不顾家很会赌钱怎么办 妈妈骂我很难听怎么办 父母管的太严怎么办 2岁宝宝哭闹不止怎么办 8岁儿童叛逆期怎么办