短信接口API
/**
* 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 { //查账户信息的http地址
private static String URI_GET_USER_INFO = "https://sms.yunpian.com/v1/user/get.json"; //智能匹配模板发送接口的http地址
private static String URI_SEND_SMS = "https://sms.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"; //编码格式。发送编码格式统一用UTF-8
private static String ENCODING = "UTF-8"; public static void main(String[] args) throws IOException, URISyntaxException { //修改为您的apikey.apikey可在官网(http://www.yuanpian.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));
} /**
* 取账户信息
*
* @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 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);
} /**
* 通过模板发送短信(不推荐)
*
* @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);
} /**
* 基于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);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return responseText;
}
}
短信接口API的更多相关文章
- asp.net mvc短信接口调用——阿里大于API开发心得
互联网上有许多公司提供短信接口服务,诸如网易云信.阿里大于等等.我在自己项目里需要使用到短信服务起到通知作用,实际开发周期三天,完成配置.开发和使用,总的说,阿里大于提供的接口易于开发,非常的方便,短 ...
- Java版阿里云通信短信发送API接口实例(新)
阿里云通信(原名阿里大于)的短信服务(Short Message Service)是阿里云为用户提供的一种通信服务的能力,支持快速发送短信验证码.短信通知等. 完美支撑双11期间2亿用户,发送6亿短信 ...
- Java 使用阿里云短信的API接口
亲们上午好,写的不好的地方还望指正.谢谢各位! 引言 短信服务(Short Message Service)是阿里云为用户提供的一种通信服务的能力,支持快速发送短信验证码.短信通知等.(我这里只讲一个 ...
- 模板短信接口调用java,pythoy版(二) 阿里大于
说明 功能:短信通知发送 + 短信发送记录查询,所有参数我没有改动,实测有效! 请自行参考 + 官方API! 短信模板示例:尊敬的${name},您的快递已在飞奔的路上,将在今天${time}送达您的 ...
- 模板短信接口调用java,pythoy版(一) 网易云信
说明 短信服务平台有很多,我只是个人需求,首次使用,算是测试用的,故选个网易(大公司). 稳定性:我只测试了15条短信... 不过前3条短信5分钟左右的延时,后面就比较快.... 我只是需要发短信,等 ...
- 短信接口调用以及ajax发送短信接口实现以及前端样式
我们短信api用的是云信使平台提供的非免费短信服务:官网提供的demo有两种,分别是function加其调用.class文件加其调用. 在这里我们用class文件加调用: 首先,ThinkPHP里面自 ...
- Thinkphp框架 -- 短信接口验证码
我用的是一款名叫 短信宝 的应用,新注册的用户可以免费3条测试短信,发现一个BUG,同个手机可以无限注册,自己玩玩还是可以的. 里面的短信接口代码什么信息都没有,感觉看得不是很明白,自己测试了一遍,可 ...
- zabbix短信接口调用
#!/bin/bash TIME=`date +%Y-%m-%d` KEY="UJK9rk50HD8du8JE8h87RUor0KERo5jk" username="za ...
- asp.net C# 实现阿里大鱼和云片网短信接口类
云片网短信通用类 public class YunpianSMS { public YunpianSMS() { } /// <summary> /// 服务器HTTP地址 /// < ...
随机推荐
- Windows共享作为公司文件服务器的案例
1.目录结构 → 主管 部门 → 员工 → Public 2.实现效果 每个部门一个目录 部门主管可以访问自己和部门员工的目录 部门员工只可访问自己的目录 公共目录Public部门所有人都可访问 3. ...
- LLVM 笔记(五)—— LLVM IR
ilocker:关注 Android 安全(新手) QQ: 2597294287 LLVM 的 IR (Intermediate Representation) 是其设计中的最重要的部分.优化器在进行 ...
- 安卓直播开源: RTMP 推流SDK
前些日子在github上提交了基于GPUImage的IOS直播推流SDK(https://github.com/runner365/GPUImageRtmpPush) 最近整理了android直播推流 ...
- JavaScript 基础回顾——数组
JavaScript是无类型语言,数组元素可以具有任意的数据类型,同一个数组的不同元素可以具有不同类型.数组的元素设置可以包含其他数组,便于模拟创建多维数组. 1.创建数组 在JavaScript中, ...
- 在 Azure HDInsight 中安装和使用 Spark
Spark本身用Scala语言编写,运行于Java虚拟机(JVM).只要在安装了Java 6以上版本的便携式计算机或者集群上都可以运行spark.如果您想使用Python API需要安装Python解 ...
- 第47课 Qt中的调色板
1. QPalette类 (1)QPalette类提供了绘制QWidget组件的不同状态所使用的颜色. (2)QPalette对象包含了3个状态的颜色描述 ①激活颜色组(Active):组件获得焦点使 ...
- python BeautifulSoup模块的简要介绍
常用介绍: pip install beautifulsoup4 # 安装模块 from bs4 import BeautifulSoup # 导入模块 soup = BeautifulSoup(ht ...
- PhpStorm XDebug 远程调试
现在我们自己公司的各种开发和测试服务器,都是使用阿里云的服务器.一些PHP的项目,无法在本地搭建完整的环境,在外网服务器上调试更方便定位问题.发现网上没有完整的关于如何配置PHPStorm和XDebu ...
- 用html5 canvas和JS写个数独游戏
为啥要写这个游戏? 因为我儿子二年级数字下册最后一章讲到了数独.他想玩儿. 因为我也想玩有提示功能的数独. 因为我也正想决定要把HTML5和JS搞搞熟.熟悉一个编程平台,最好的办法,就是了解其原理与思 ...
- JavaScript----Js操控-HTML5 <progress> 标签
Js操控----HTML5 <progress> 标签 简单模拟下下载进度跑条 <h4>加载进度</h4> <input type="button& ...