云片网实现短信验证码

本文介绍如何利用云片网的短信接口实现用户注册和重置密码时的短信验证码功能。包括添加短信签名、模板审核步骤及Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 项目开发中,用户注册和重置密码要发送短信验证码。

        找了个第三方的服务商——云片网,看了看上面的API实例代码,实现起来非常简单,但中间还是有一些小波折,今天来给大家扫除障碍,完美实现短信验证~

        首先,进入云片网,注册一个账号,然后进入“短信发送”菜单的“添加签名”页面,添加公司名称,如图:

        

        之后,还是在“短信发送”菜单,进入“添加模板”,添加发送的短信内容,通过审核后,您的短信内容才能发送成功,如图:

       

       接着,点击右上角的退出按钮,进入API开发文档,查看示例。这里给出了定义好的代码模板,我们直接拿过来用就可以了,我用的它推荐的智能匹配模板短信接口:

    注:本样例基于 Apache HttpClient v4.3 , 样例项目包含代码、依赖 jar包和pom.xml配置

Windows用户请注意:下载的代码及说明文件都是UTF-8格式编码,且换行风格为Linux的。 用记事本或写字板直接打开显示会不正常。建议将其导入(或复制)到Eclipse等IDE, 并将项目编码格式设置为UTF-8后再打开这些文件查看。若直接查看请用Notepad++、EditPlus等编辑器。

/**
* Created by bingone on 15/12/16.
*/

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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 java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 短信http接口的java代码调用示例
* 基于Apache HttpClient 4.3
*
* @author songchao
* @since 2015-04-03
*/

public class JavaSmsApi {

	private static String API_KEY="XXXXXXXXXXXXXXXXXXXXXX";
	
    //查账户信息的http地址
    private static String URI_GET_USER_INFO = "https://sms.yunpian.com/v2/user/get.json";

    //智能匹配模板发送接口的http地址
    private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";

    //模板发送接口的http地址
    private static String URI_TPL_SEND_SMS = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";

    //发送语音验证码接口的http地址
    private static String URI_SEND_VOICE = "https://voice.yunpian.com/v2/voice/send.json";

    //绑定主叫、被叫关系的接口http地址
    private static String URI_SEND_BIND = "https://call.yunpian.com/v2/call/bind.json";

    //解绑主叫、被叫关系的接口http地址
    private static String URI_SEND_UNBIND = "https://call.yunpian.com/v2/call/unbind.json";

    //编码格式。发送编码格式统一用UTF-8
    private static String ENCODING = "UTF-8";

    public static void main(String[] args) throws IOException, URISyntaxException {

        //修改为您的apikey.apikey可在官网(http://www.yunpian.com)登录后获取
        String apikey = "xxxxxxxxxxxxxxxxxxxxx";

        //修改为您要发送的手机号
        String mobile = "130xxxxxxxx";

        /**************** 查账户信息调用示例 *****************/
        System.out.println(JavaSmsApi.getUserInfo(apikey));

        /**************** 使用智能匹配模板接口发短信(推荐) *****************/
        //设置您要发送的内容(内容必须和某个模板匹配。以下例子匹配的是系统提供的1号模板)
        String text = "【云片网】您的验证码是1234";
        //发短信调用示例
        // System.out.println(JavaSmsApi.sendSms(apikey, text, mobile));

        /**************** 使用指定模板接口发短信(不推荐,建议使用智能匹配模板接口) *****************/
        //设置模板ID,如使用1号模板:【#company#】您的验证码是#code#
        long tpl_id = 1;
        //设置对应的模板变量值

        String tpl_value = URLEncoder.encode("#code#",ENCODING) +"="
        + URLEncoder.encode("1234", ENCODING) + "&"
        + URLEncoder.encode("#company#",ENCODING) + "="
        + URLEncoder.encode("云片网",ENCODING);
        //模板发送的调用示例
        System.out.println(tpl_value);
        System.out.println(JavaSmsApi.tplSendSms(apikey, tpl_id, tpl_value, mobile));

        /**************** 使用接口发语音验证码 *****************/
        String code = "1234";
        //System.out.println(JavaSmsApi.sendVoice(apikey, mobile ,code));

        /**************** 使用接口绑定主被叫号码 *****************/
        String from = "+86130xxxxxxxx";
        String to = "+86131xxxxxxxx";
        Integer duration = 30*60;// 绑定30分钟
        //        System.out.println(JavaSmsApi.bindCall(apikey, from ,to , duration));

        /**************** 使用接口解绑主被叫号码 *****************/
        //        System.out.println(JavaSmsApi.unbindCall(apikey, from, to));
    }

    /**
    * 取账户信息
    *
    * @return json格式字符串
    * @throws java.io.IOException
    */

    public static String getUserInfo(String apikey) throws IOException, URISyntaxException {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey);
        return post(URI_GET_USER_INFO, params);
    }

    /**
    * 智能匹配模板接口发短信
    *
    * @param apikey apikey
    * @param text    短信内容
    * @param mobile  接受的手机号
    * @return json格式字符串
    * @throws IOException
    */

    public static String sendSms(String text, String mobile) throws IOException {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey",API_KEY);
        params.put("text", text);
        params.put("mobile", mobile);
        return post(URI_SEND_SMS, params);
    }

    /**
    * 通过模板发送短信(不推荐)
    *
    * @param apikey    apikey
    * @param tpl_id     模板id
    * @param tpl_value  模板变量值
    * @param mobile     接受的手机号
    * @return json格式字符串
    * @throws IOException
    */

    public static String tplSendSms(String apikey, long tpl_id, String tpl_value, String mobile) throws IOException {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey);
        params.put("tpl_id", String.valueOf(tpl_id));
        params.put("tpl_value", tpl_value);
        params.put("mobile", mobile);
        return post(URI_TPL_SEND_SMS, params);
    }

    /**
    * 通过接口发送语音验证码
    * @param apikey apikey
    * @param mobile 接收的手机号
    * @param code   验证码
    * @return
    */

    public static String sendVoice(String apikey, String mobile, String code) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey);
        params.put("mobile", mobile);
        params.put("code", code);
        return post(URI_SEND_VOICE, params);
    }

    /**
    * 通过接口绑定主被叫号码
    * @param apikey apikey
    * @param from 主叫
    * @param to   被叫
    * @param duration 有效时长,单位:秒
    * @return
    */

    public static String bindCall(String apikey, String from, String to , Integer duration ) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey);
        params.put("from", from);
        params.put("to", to);
        params.put("duration", String.valueOf(duration));
        return post(URI_SEND_BIND, params);
    }

    /**
    * 通过接口解绑绑定主被叫号码
    * @param apikey apikey
    * @param from 主叫
    * @param to   被叫
    * @return
    */
    public static String unbindCall(String apikey, String from, String to) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey);
        params.put("from", from);
        params.put("to", to);
        return post(URI_SEND_UNBIND, params);
    }

    /**
    * 基于HttpClient 4.3的通用POST方法
    *
    * @param url       提交的URL
    * @param paramsMap 提交<参数,值>Map
    * @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;
        }
}


需要注意的是,直接调用后会报错,因为云片网会给每个用户分配一个短信白名单的IP,要登录其官网,添加IP后才能成功。这个IP应该是每换一台数据库服务器就会改变一次,我的本机和阿里云上都有数据库,也就是有两个IP才能保证换库调试成功。如图:

         

         至于验证码的生成规则,我这里就不啰嗦了,自己定义就可以了。

         好了,现在测试一下吧,你的手机是不是收到了验证短信?


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值