253短信发送(国内短信)
来源:互联网 发布:计算机编程自学网站 编辑:程序博客网 时间:2024/06/05 20:57
app项目中有时会用到第三方文档例如短信的发送,接触到创蓝253短信发送总结一下
一、引入文件
1、到253网站下载需要引入的api
地址:https://www.253.com/api-docs.html
其中的文档说明就特别详细,结合项目我进行了一下整改,方便项目中调用(以验证码的发送为例)
package import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.net.URLDecoder;import java.text.MessageFormat;import java.util.HashMap;import java.util.Map;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpStatus;import org.apache.commons.httpclient.NameValuePair;import org.apache.commons.httpclient.SimpleHttpConnectionManager;import org.apache.commons.httpclient.URI;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.params.HttpClientParams;/** * * @ClassName ChuanglanSMS * @Description TODO(创蓝发送短信) * @author * @Date 2017年10月11日 下午4:07:51 * @version 1.0.0 */public class ChuanglanSMS {/** * 发送短信验证码 * @param telephone 要发送的手机号 * @param code 验证码 */public static Map<String, Object> send(String telephone, String code){Map<String, Object> resultMap = new HashMap<String, Object>();try {if(StringUtil.isNotEmpty(telephone) && StringUtil.isNotEmpty(code)){String resultCode = "";//返回状态//发送内容String sendContent = ChuanglanConfig.SMS_CONTENT;sendContent = MessageFormat.format(sendContent, code);//验证码String returnStr = ChuanglanSMS.batchSend(ChuanglanConfig.SMS_CHINA_URL, ChuanglanConfig.SMS_CHINA_ACCOUNT, ChuanglanConfig.SMS_CHINA_PASSWORD, telephone, sendContent, "1", null);if(returnStr.indexOf("\n")!=-1){//包含换行符 resultCode = returnStr.substring(returnStr.indexOf(",")+1,returnStr.indexOf("\n"));}else{ resultCode = returnStr.substring(returnStr.indexOf(",")+1);}//返回情况处理if("0".equals(resultCode)){resultMap.put("code", "200");resultMap.put("msg", "发送成功");}else if("107".equals(resultCode)){resultMap.put("code", "300");resultMap.put("msg", "手机号码格式错误");}else{resultMap.put("code", "300");resultMap.put("msg", "发送短信验证码失败");}}else{resultMap.put("code", "300");resultMap.put("msg", "手机号码不能为空");}return resultMap;} catch (Exception e) {e.printStackTrace();resultMap.put("code", "300");resultMap.put("msg", "发送短信验证码失败");return resultMap;}}/** * * @param url 应用地址,类似于http://ip:port/msg/ * @param un 账号 * @param pw 密码 * @param phone 手机号码,多个号码使用","分割 * @param msg 短信内容 * @param rd 是否需要状态报告,需要1,不需要0 * @return 返回值定义参见HTTP协议文档 * @throws Exception */public static String batchSend(String url, String un, String pw, String phone, String msg,String rd, String ex) throws Exception {HttpClient client = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager(true));GetMethod method = new GetMethod();try {URI base = new URI(url, false);method.setURI(new URI(base, "send", false));method.setQueryString(new NameValuePair[] { new NameValuePair("un", un),new NameValuePair("pw", pw), new NameValuePair("phone", phone),new NameValuePair("rd", rd), new NameValuePair("msg", msg),new NameValuePair("ex", ex), });int result = client.executeMethod(method);if (result == HttpStatus.SC_OK) {InputStream in = method.getResponseBodyAsStream();ByteArrayOutputStream baos = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = in.read(buffer)) != -1) {baos.write(buffer, 0, len);}return URLDecoder.decode(baos.toString(), "UTF-8");} else {throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());}} finally {method.releaseConnection();}}/** * * @Description (测试) * @param args */public static void main(String[] args) {String url = "";// 应用地址String un = "";// 账号String pw = "";// 密码String phone = "";// 手机号码,多个号码使用","分割String msg = "【签名】您好,你的验证码是123456";// 短信内容String rd = "1";// 是否需要状态报告,需要1,不需要0String ex = null;// 扩展码try {//直接按这个调用Map<String, Object> resultMap = ChuanglanSMS.send("15805480231","123456");System.out.println(resultMap.get("code")+":"+resultMap.get("msg"));// TODO 处理返回值,参见HTTP协议文档//String returnString = ChuanglanSMS.batchSend(url, un, pw, phone, msg, rd, ex);//System.out.println(returnString);} catch (Exception e) {// TODO 处理异常e.printStackTrace();}}}
2、常量引用类
public class ChuanglanConfig {//应用地址public static String SMS_CHINA_URL = "";//内容public static String SMS_CONTENT = "【签名】您好,您的验证码是{0}";//账号public static String SMS_CHINA_ACCOUNT = "";//密码public static String SMS_CHINA_PASSWORD = "";}
阅读全文
0 0
- 253短信发送(国内短信)
- 发送短信(转贴)
- android短信加密(发送加密短信,解密本地短信)
- 发送短信
- 发送短信
- 发送短信
- 短信发送
- 发送短信
- 发送短信
- 短信发送。
- 发送短信
- 发送短信
- 短信发送
- 发送短信
- 短信发送
- 发送短信
- 短信发送
- 短信发送
- C#预处理指令
- 排序算法总结
- js常用的json操作
- 嵌入式视频处理基础(四)
- Oracle触发器的使用及:OLD的用法
- 253短信发送(国内短信)
- VMWare虚拟机“锁定文件失败”、“打不开磁盘”或者“它所依赖的某个快照磁盘”的解决办法
- 用JS获取地址栏参数的方法(超级简单)
- 通用块层和SCSI层--块设备队列
- 优秀工程师-UISlider
- java高并发案例详细讲解
- 第七周||数据结构之自建算法库——链队(链式队列)
- 关于overflow适配IE的问题
- 第7周项目1 建立顺序环形队列算法库