发送短信验证码


public Object noteCode(HttpServletResponse response, HttpServletRequest request) {
String phone = StringUtils.defaultIfEmpty(request.getParameter("phone"), "");
boolean sendResults = true;
boolean sendResult = false;
JSONObject result = new JSONObject();
if (sendResults) {
// 发送短信
String genSmscode = nextInt(1000, 9999) + "";
// 调用发送短信接口
String resultStr = SMSHelper.sendSMS("您的手机验证码是:" + genSmscode, phone);
if ("ok".equalsIgnoreCase(resultStr)) {
sendResult = true;
} else {
// log.error();
}
Date sendDate = new Date();
if (sendResult) {
HttpSession session = request.getSession();
session.setAttribute("sendSmscodeSession", genSmscode);
session.setAttribute("sendSmsTimeSession", sendDate.getTime() + "");
result.put("success", true);
} else {
result.put("success", false);
result.put("message", "验证码发送失败!!");
}


} else {
result.put("success", false);
result.put("message", "您获取验证码的次数过于频繁!请稍后再试!谢谢!");
}
return result;
}

public static int nextInt(int min, int max) {
Random rand = new Random();
int tmp = Math.abs(rand.nextInt());
int result = tmp % (max - min + 1) + min;
return result;
}

public static String sendSMS(String content, String tel) {
String s = "ok";
try {
String service = "Send.aspx";
content = content + "你好";
if (tel.indexOf(';') > 0) {
service = "BatchSend.aspx";
} else {
if (StringUtils.isEmpty(tel) || tel.length() != 11) {
return "手机号码格式不正确!";
}
}
String serviceURL = "http://qxt.ccme.cc/";
String company = "zhangsan";
String password = "123456";
String sRequestURL = serviceURL + service + "?CorpID=" + company + "&Pwd=" + password;
String sendcontent = URLEncoder.encode(content, "GBK");
sRequestURL += "&Content=" + sendcontent + "&Mobile=" + tel + "&Cell=&SendTime=";
URL url = new URL(sRequestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
String sResponse = br.readLine();
int iret = Integer.parseInt(sResponse);
if (iret < 0) {
s = "短信发送失败!失败原因:" + sResponse;
}
} else {
s = "无法访问短信服务端口。";
}
br.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
s = "网络连接异常";
}
return s;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值