验证码发送手机短信功能

1.需要在短信服务器购买平台(此处以创蓝云通讯为例:253.com)

2.购买拿到用户名以及密码后就可以写代码开发了;

3.贴完整代码如下:(在springMvc下开发的)

创建实例类:

package net.shopxx.entity;

import java.util.Random;

/**
*
* @author tianyh
* @date 2017年4月15日 下午3:41:44
* @Title: SmsSingleRequest
* @ClassName: SmsSingleRequest
* @Description:普通短信发送
*/
public class SmsSendRequest {
/**
* 用户账号,必填
*/
private String account;
/**
* 用户密码,必填
*/
private String password;
/**
* 短信内容。长度不能超过536个字符,必填
*/
private String msg;
/**
* 机号码。多个手机号码使用英文逗号分隔,必填
*/
private String phone;


/**
* 定时发送短信时间。格式为yyyyMMddHHmm,值小于或等于当前时间则立即发送,默认立即发送,选填
*/
private String sendtime;
/**
* 是否需要状态报告(默认false),选填
*/
private String report;
/**
* 下发短信号码扩展码,纯数字,建议1-3位,选填
*/
private String extend;
/**
* 该条短信在您业务系统内的ID,如订单号或者短信发送记录流水号,选填
*/
private String uid;

public SmsSendRequest() {

}
public SmsSendRequest(String account, String password, String msg, String phone) {
super();
this.account = account;
this.password = password;
this.msg = msg;
this.phone = phone;
}
public SmsSendRequest(String account, String password, String msg, String phone, String sendtime) {
super();
this.account = account;
this.password = password;
this.msg = msg;
this.phone = phone;
this.sendtime=sendtime;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSendtime() {
return sendtime;
}
public void setSendtime(String sendtime) {
this.sendtime = sendtime;
}
public String getReport() {
return report;
}
public void setReport(String report) {
this.report = report;
}
public String getExtend() {
return extend;
}
public void setExtend(String extend) {
this.extend = extend;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}

public static String createRandNum() {
Random random = new Random();
StringBuffer sb = new StringBuffer();
for(int i = 0; i <= 5; i++) {
String s = random.nextInt(10) + "";
sb.append(s);
}
return sb.toString();
}
}

 

创建实例类:

package net.shopxx.entity;
/**
*
* @author tianyh
* @date 2017年4月15日 下午3:41:59
* @Title: SmsSingleResponse
* @ClassName: SmsSingleResponse
* @Description:普通短信发送响应实体类
*/
public class SmsSendResponse {
/**
* 响应时间
*/
private String time;
/**
* 消息id
*/
private String msgId;
/**
* 状态码说明(成功返回空)
*/
private String errorMsg;
/**
* 状态码(详细参考提交响应状态码)
*/
private String code;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getMsgId() {
return msgId;
}
public void setMsgId(String msgId) {
this.msgId = msgId;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "SmsSingleResponse [time=" + time + ", msgId=" + msgId + ", errorMsg=" + errorMsg + ", code=" + code
+ "]";
}


}

创建实例类:

 

package net.shopxx.entity;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/**
*
* @author tianyh
* @date 2017年4月15日 下午3:27:08
* @Title: ChuangLanSmsUtil
* @ClassName: ChuangLanSmsUtil
* @Description:HTTP 请求
*/
public class ChuangLanSmsUtil {

/**
*
* @author tianyh
* @date 2017年4月15日 下午3:27:04
* @Title sendSmsByPost
* @Description
* @param path
* @param postContent
* @return String
* @throws
*/
public static String sendSmsByPost(String path, String postContent) {
URL url = null;
try {
url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
// conn.setConnectTimeout(10000);//连接超时 单位毫秒
// conn.setReadTimeout(2000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestProperty("Content-Type", "application/json");
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
printWriter.write(postContent);
// flush输出流的缓冲
printWriter.flush();

StringBuilder sb = new StringBuilder();
int httpRspCode = httpURLConnection.getResponseCode();
if (httpRspCode == HttpURLConnection.HTTP_OK) {
// 开始获取数据
BufferedReader br = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();

}

} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

 

在springMVC中写入的控制层方法:

/**
* 验证码发送手机短信
*/
@RequestMapping("/postIdentifyingCode")
public ResponseEntity<?> postIdentifyingCode(String phone,ModelMap model,HttpServletRequest request, HttpServletResponse response){

Setting setting = SystemUtils.getSetting();
String pswd=setting.getPswd();
String account=setting.getAccount();
HttpSession session = request.getSession();
String smsNew=SmsSendRequest.createRandNum();
// 普通短信地址
String smsSingleRequestServerUrl = "http://vsms.253.com/msg/send/json";
// 短信内容
String msg = "您正在使用手机微信端注册商城用户,如果不是本人操作,请勿泄露验证码,你的验证码是"+smsNew+"";
//手机号码
// phone = "13764187236";

SmsSendRequest smsSingleRequest = new SmsSendRequest(account, pswd, msg, phone);

String requestJson = JSON.toJSONString(smsSingleRequest);

System.out.println("before request string is: " + requestJson);

String responseNew = ChuangLanSmsUtil.sendSmsByPost(smsSingleRequestServerUrl, requestJson);

System.out.println("response after request result is :" + response);

SmsSendResponse smsSingleResponse = JSON.parseObject(responseNew, SmsSendResponse.class);

String time=smsSingleResponse.getTime();
String msgId=smsSingleResponse.getMsgId();
String code=smsSingleResponse.getCode();
String errormsg=smsSingleResponse.getErrorMsg();
System.out.println(time+"?"+msgId+"?"+code+"?"+errormsg);
session.setAttribute("timeNew", time);
session.setAttribute("msgId", msgId);
session.setAttribute("phone", phone);
session.setAttribute("smsNew", smsNew);
System.out.println("response toString is :" + smsSingleResponse);
model.addAttribute("smsCode", smsNew);
return Results.OK;
}

 

使用的发送短信中的jar包:fastjson-1.2.14.jar

转载于:https://www.cnblogs.com/Generalwake/p/9850576.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值