1.我们都知道,短信验证码需要去调用短信平台的接口,
2.调用这个接口需要的参数(url---短信发送地址,msg---短信发送内容即验证码)
3.自己写一个随机数做为---》验证码
4.自己创建新的类,用来调用短信验证
5.结合自己的类,调用验证码
废话不多说;直接撸代码:
public final class SmsUtil {
public static void sendSms(final String url, final String smsContent) throws Exception {
if (null == url || 0 == url.length())
throw new IllegalArgumentException("短信接口連接地址不能為空");
if (null == smsContent || 0 == smsContent.length())
throw new IllegalArgumentException("短信發送內容不能為空");
try {
URL connection = new URL(url);
HttpURLConnection conn = (HttpURLConnection) connection.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ");
conn.setRequestProperty("Content-type", "text/xml;charset=UTF-8");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
conn.getOutputStream()));
bw.write(smsContent);
bw.close();
BufferedReader br = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String out = null;
while ((out = br.readLine()) != null) {
System.out.println(out);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
public static void main(String[] args) throws Exception {
//随机数生成
String rd_code = WCM_utils_SequenceUtils.rd_code();
String url = "http://10.10.100.100:0000/smsinterface/sendInterface/sendSms_xml.do";.。地址:自己本地的或者测试的或者线上的
// 短信驗證碼
String code = "<req op='sms_01send'>" +
"<h>" +
"<ver>1.0</ver>" +
"<user>admin</user>" +
"<pass>admin</pass>" +
"</h>" +
"<items>" +
"<item>" +
"<id>1</id>" + id---》一般也用随机数,我用的12位,自己写个
"<content>“+code +”</content>" + code---->随机数验证码
"<tele>“+前端手机号+”</tele>" +
"</item>" +
"</items>" +
"</req>";
// 這裡是短信接口項目當前的訪問地址URL
sendSms(url,code);
}
}
注释:吧这两个方法写在一个类里面,可以在自己的类里面调用,获取验证码
自己的类---关键性代码:
//未绑定,前端给手机号
String per_phone = request.getParameter("per_phone");
//随机数生成
String rd_code = WCM_utils_SequenceUtils.rd_code();
String dx_id = WCM_utils_SequenceUtils.dx_id();
String url = "http://10.19.105.131:7001/smsinterface/sendInterface/sendSms_xml.do";
String code = "<req op='sms_01send'>" +
"<h>" +
"<ver>1.0</ver>" +
"<user>admin</user>" +
"<pass>admin</pass>" +
"</h>" +
"<items>" +
"<item>" +
"<id>"+dx_id+"</id>" +
"<content>"+rd_code+"</content>" +
"<tele>"+per_phone+"</tele>" +
"</item>" +
"</items>" +
"</req>";
SmsUtil.sendSms(url, code);
//短信平台获取验证码per_phone
String dx_cd = "rd_code";
//前端传入的验证码
String qd_dx_cd = request.getParameter("dx_code");
if(dx_cd.equals("qd_dx_cd")){
条件符合则跳转另一个页面
}