其实很多人都在用短信平台做这个注册的短信验证这块,以下是一个短信猫做短信验证的例子。
有具体的注解,可以尝试用用。
/**
* 获取验证码*/
public void sendCode()
{
try
{
// 获取手机号码
String phoneNo = ((String[])ActionContext.getContext().getParameters().get("phoneNo"))[0].trim();
// 获取6位sms验证码
String SMSCode = SMSCodeUtil.createSMSCode();
// 将sms验证码放入session中
ActionContext.getContext().getSession().put("SMSCode",SMSCode);
// 建立currentTimeMillis定时器
ActionContext.getContext().getSession().put("SMSTime",System.currentTimeMillis());
String smsContext = "[卖炭网]注册验证码:" + SMSCode + ",该验证码有效期为5分钟。如非本人操作,请忽略此短信。";
// 建立流对象
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
SendRecieveSms.getSendSms(phoneNo, smsContext, inputStream);
getResponse().getWriter().print("1");
}
catch (Exception e)
{
e.printStackTrace();
}
}
//这里较常用的包就不贴出来了
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;//短信猫的包
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
//提供短信猫的接口类
public class SendRecieveSms {
private String endPoint;
private String namespace;
private String method;
private static SendRecieveSms sendRecieveSms = null;
static Properties p = new Properties();
public SendRecieveSms(String endPoint, String namespace, String method) {
this.endPoint = endPoint;
this.namespace = namespace;
this.method = method;
}
/**
* 发短信部分
*/
public String sendSms(String phone, String msg, String com){
String strRtn = null;
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(this.endPoint));
call.setOperationName(new QName(this.namespace, method));
call.addParameter(new QName(this.namespace, "phone"),XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter(new QName(this.namespace, "msg"),XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter(new QName(this.namespace, "com"),XMLType.XSD_STRING, ParameterMode.IN);
call.setUseSOAPAction(true);
call.setSOAPActionURI(namespace + method);
call.setReturnType(XMLType.XSD_STRING);
strRtn = call.invoke(new Object[] { phone, msg , com }).toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
return strRtn;
}
/**
* @param args
* 在此处传入三个参数
* 分别是电话号码、短信内容、流对象
*/
public static String getSendSms(String phoneNo, String smsContext, InputStream inputStream)
{
synchronized (SendRecieveSms.class)
{
if(sendRecieveSms == null)
{
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
sendRecieveSms = new SendRecieveSms(
"http://"+p.getProperty("message.service.host")+":"+p.getProperty("message.service.port")+"/Service.asmx",
"http://qxtweb/webservices", "Sendmsg");
}
return sendRecieveSms.sendSms(phoneNo, smsContext, p.getProperty("message.service.com"));
}
}
/**
* @param args
* 测试main方法
*/
public static void main(String[] args) {
SendRecieveSms lmm = new SendRecieveSms(
"http://localhost:4305/Service.asmx",
"http://qxtweb/webservices", "Sendmsg");
System.out.println(lmm.sendSms("18706897798", "您好,您的验证码为:【9645】", "COM3"));
}
}