短信猫实现短信验证小例子

 其实很多人都在用短信平台做这个注册的短信验证这块,以下是一个短信猫做短信验证的例子。

有具体的注解,可以尝试用用。




 /**

     * 获取验证码
     */
    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"));
}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值