短信验证码
短信验证码
package com.gwCloud.mobile.mqtt;
/**
- 发送短信
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.NameValuePair;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.gwCloud.core.controller.BaseController;
import com.gwCloud.core.route.RouteBind;
import com.gwCloud.model.base.ShortMessageParameter;
import com.gwCloud.model.base.Verificationcode;
@RouteBind(path="/mobile/sms")
public class SmsController extends BaseController {
//此方法在服务可能得不到端口回应,游戏服务器自带的IE浏览器不能可以调用短信接口但无法发送短信
public static String sendPost_old(String url, String param) throws UnsupportedEncodingException {
PrintWriter out = null;
BufferedReader in = null;
String result = “”;
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty(“accept”, “/”);
conn.setRequestProperty(“connection”, “Keep-Alive”);
conn.setRequestProperty(“user-agent”,
“Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)”);
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
System.out.println(“result=”+result);
}
} catch (Exception e) {
System.out.println(“发送 POST 请求出现异常!”+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
public static String sendPost(String url, ShortMessageParameter param) {
//类型转换
Field[] fields = param.getClass().getDeclaredFields();
//键值对
List<NameValuePair> list = new ArrayList<NameValuePair>();
BasicNameValuePair nameValuePair = null;
for(Field field : fields) {
field.setAccessible(true);
try {
nameValuePair = new BasicNameValuePair(field.getName(),(String)field.get(param));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list.add(nameValuePair);
}
UrlEncodedFormEntity formEntity = null;
try {
formEntity = new UrlEncodedFormEntity(list,"utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 获得Http客户端
HttpClient httpClient = HttpClientBuilder.create().build();
//创建post请求
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(formEntity);
try {
// 由客户端发送请求
HttpResponse response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
}
//return EntityUtils.toString(responseEntity);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 前端短信调用接口
*/
public void domainSendMessage() {
String code = getPara("code");
if (isMobileNO(code) == true) {
String url = "http://193.112.6.53:8088/sms.aspx?action=send" ;
//生成四位随机数
Random random = new Random();
String result="";
for (int i=0;i<4;i++)
{
result+=random.nextInt(10);
}
ShortMessageParameter messageParameter = new ShortMessageParameter();
messageParameter.setUserid("***");
messageParameter.setAccount("***");
messageParameter.setPassword("***");
messageParameter.setMobile(code);
messageParameter.setContent("【吉物科技】您的验证码是:"+result+",有效时间为1分钟。若非您本人操作,可忽略本消息");
messageParameter.setExtno("8888");
sendPost(url, messageParameter);
saveCodeMessge(code,result);
renderJson(true);
}else {
//手机号码不合法
System.out.println("手机号码不合法");
renderJson(false);
}
}
/**
* 将短信验证码存储在数据库中
*/
public String saveCodeMessge(String phoneNum,String result) {
Verificationcode verificationcode = getModel(Verificationcode.class,"") ;
verificationcode.setCode(phoneNum);
verificationcode.setVerificationCode(result);
//后端做判空处理
if (phoneNum == null || phoneNum.equals("")) {
return "保存失败" ;
}else {
verificationcode.save() ;
System.out.println(verificationcode.getCode());
System.out.println(verificationcode.getVerificationCode());
return "保存成功" ;
}
}
/**
* 验证手机号码是否是合法的
*/
public static boolean isMobileNO(String mobile){
if (mobile.length() != 11)
{
return false;
}else{
/**
* 移动号段正则表达式
*/
String pat1 = "^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
/**
* 联通号段正则表达式
*/
String pat2 = "^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
/**
* 电信号段正则表达式
*/
String pat3 = "^((133)|(153)|(177)|(18[0,1,9])|(149))\\d{8}$";
/**
* 虚拟运营商正则表达式
*/
String pat4 = "^((170))\\d{8}|(1718)|(1719)\\d{7}$";
Pattern pattern1 = Pattern.compile(pat1);
Matcher match1 = pattern1.matcher(mobile);
boolean isMatch1 = match1.matches();
if(isMatch1){
return true;
}
Pattern pattern2 = Pattern.compile(pat2);
Matcher match2 = pattern2.matcher(mobile);
boolean isMatch2 = match2.matches();
if(isMatch2){
return true;
}
Pattern pattern3 = Pattern.compile(pat3);
Matcher match3 = pattern3.matcher(mobile);
boolean isMatch3 = match3.matches();
if(isMatch3){
return true;
}
Pattern pattern4 = Pattern.compile(pat4);
Matcher match4 = pattern4.matcher(mobile);
boolean isMatch4 = match4.matches();
if(isMatch4){
return true;
}
return false;
}
}
}
下一篇: 如何升级H5混合开发的app