文章标题
来源:互联网 发布:asp.net和php对比 编辑:程序博客网 时间:2024/06/05 00:50
云片网短信接口
- 注册云片账号
- 开发者认证(个人/企业),输入相关信息、上传相关证件,提交审核
- 一个账号生成唯一的秘钥,可以有多个子账号
- 新增短信签名(加在短信的开头,【腾讯】),提交审核
- 新增模板(发送验证码、系统通知、活动通知的模板),提交审核
- 开始接触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 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- Java1.8 HashMap
- android shape 属性详细解析(mmp这波整理完以后都不用麻烦了)
- MySQL数据库(31)
- SSL协议全面剖析
- ambari Metric自研
- 文章标题
- js中的一些常见的事件。
- AngularJS的作用域$scope监视
- Java并发读书笔记(四)
- ORACLE数据库存储空间使用情况查询
- 最小生成树的Prim算法
- (转)深入Ambari Metrics 机制分析
- 利用透视变换做单目测量
- myeclipse10配置maven