需要jar包
commons-codec-1.9.jar
目前手機(jī)注冊(cè)登錄是時(shí)下非常流行的一種交互方式旁壮,出售短信包的平臺(tái)以及對(duì)應(yīng)的API有很多,這里只以其中一個(gè)為例荣恐,秒嘀
Config.java
package test;
public class Config{
public static final String BASE_URL = "https://api.miaodiyun.com/20150822";
// 此參數(shù)在開(kāi)發(fā)者注冊(cè)后系統(tǒng)自動(dòng)生成啤贩,去對(duì)應(yīng)平臺(tái)登錄進(jìn)行查看
public static final String ACCOUNT_SID = "91252829202f42ccb380369b886a481b";
// 此參數(shù)在開(kāi)發(fā)者注冊(cè)后系統(tǒng)自動(dòng)生成,去對(duì)應(yīng)平臺(tái)登錄進(jìn)行查看
public static final String AUTH_TOKEN = "424a8c02ba794b3aabad9eb00c3e4db7";
public static final String RESP_DATA_TYPE = "json";
}
HttpUtil.java
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.codec.digest.DigestUtils;
public class HttpUtil
{
public static String createCommonParam()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timestamp = sdf.format(new Date());
String sig = DigestUtils.md5Hex(Config.ACCOUNT_SID + Config.AUTH_TOKEN + timestamp);
return "×tamp=" + timestamp + "&sig=" + sig + "&respDataType=" + Config.RESP_DATA_TYPE;
}
public static String post(String url, String body)
{
System.out.println("url:" + System.lineSeparator() + url);
System.out.println("body:" + System.lineSeparator() + body);
String result = "";
try
{
OutputStreamWriter out = null;
BufferedReader in = null;
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(5000);
conn.setReadTimeout(20000);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(body);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = "";
boolean firstLine = true;
while ((line = in.readLine()) != null)
{
if (firstLine)
{
firstLine = false;
} else
{
result += System.lineSeparator();
}
result += line;
}
} catch (Exception e)
{
e.printStackTrace();
}
return result;
}
public static String postHuiDiao(String url, String body)
{
String result = "";
try
{
OutputStreamWriter out = null;
BufferedReader in = null;
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(5000);
conn.setReadTimeout(20000);
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(body);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = "";
boolean firstLine = true;
while ((line = in.readLine()) != null)
{
if (firstLine)
{
firstLine = false;
} else
{
result += System.lineSeparator();
}
result += line;
}
} catch (Exception e)
{
e.printStackTrace();
}
return result;
}
}
IndustrySMS.java
package test;
import java.net.URLEncoder;
import test.Config;
import test.HttpUtil;
public class IndustrySMS
{
private static String operation = "/industrySMS/sendSMS";
private static String accountSid = Config.ACCOUNT_SID;
// 要發(fā)送到的手機(jī)號(hào)碼
private static String to = "13386895356";
// 對(duì)應(yīng)模板信息嘹吨,驗(yàn)證碼可以選擇隨機(jī)生成
private static String smsContent = "【xxxxx】您的信驗(yàn)證碼為123456,請(qǐng)?jiān)?分鐘內(nèi)使用";
/**
* 發(fā)送信息的方法
*/
public static void execute()
{
String tmpSmsContent = null;
try{
tmpSmsContent = URLEncoder.encode(smsContent, "UTF-8");
}catch(Exception e){
}
String url = Config.BASE_URL + operation;
String body = "accountSid=" + accountSid + "&to=" + to + "&smsContent=" + tmpSmsContent
+ HttpUtil.createCommonParam();
String result = HttpUtil.post(url, body);
System.out.println("result:" + System.lineSeparator() + result);
}
}
Test.java
package test;
public class Test{
public static void main(String[] args){
IndustrySMS.execute();
}
}
驗(yàn)證思路
將手機(jī)驗(yàn)證碼存入session中境氢,利用ajax請(qǐng)求獲取發(fā)送的驗(yàn)證碼蟀拷,前臺(tái)頁(yè)面啟動(dòng)定時(shí)器,在60s內(nèi)可以與從后臺(tái)取得的驗(yàn)證碼進(jìn)行對(duì)比驗(yàn)證萍聊,超時(shí)后问芬,將獲取到的驗(yàn)證碼清空