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