短信发送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
- 短信发送api示例
- Android发送短信示例
- SMS API发送短信
- 利用j2meAPI发送短信示例
- 利用j2meAPI发送短信示例
- AT指令发送短信示例
- Android简单短信发送示例
- 阿里云发送短信API
- 短信发送API(SendSms)---PHP
- java调用发送短信API
- Android发送接收短信的代码示例
- Android发送接收短信的代码示例
- Android定时发送短信完整详细示例
- PHP发送短信的示例代码
- MTK发送短信的一个api
- 使用飞信API 发送 短信
- 调用Android系统API发送短信,打电话
- Rexsee API介绍:SMSSender短信发送
- 程序运行完后不让窗口一闪而过
- 将一组单词存储在一个数组中,按正序和倒序分别显示单词
- 如何在局域网访问Tomcat项目(网站搭建过程中可能会遇到这个问题)
- Comb+All-pass filter组合Reverberation中Comb feedback系数校正
- java.sql.SQLException: Unknown system variable 'tx_read_only'
- 短信发送api示例
- T-SQL Part IV: ORDER BY
- JAVAScript和JAVA有区别吗
- 进程与线程的一个简单解释
- 如何打造工程师文化?这10件事情值得去做
- iOS开发之使用XMPPFramework实现即时通信(一)
- 移动跨平台开发---cordova(四)
- c++第六次上机作业
- 正则表达式中各种字符的含义