您的位置:首页 > 其它

短信发送api示例

2016-05-20 11:11 459 查看
首先说一下短信验证的使用场景。

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);
}


短信发送,算是一块成熟的技术。所以肯定有第三方程序已经提供了。我们需要做的就是找到这些程序,把他们提供的使用到我们的项目中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  短信