短信驗(yàn)證碼登錄流程思路及詳細(xì)步驟
Part2首先添加一個(gè)jar包
工具類會(huì)用到
<!--秒滴云的jar包-->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>Part3編寫簡(jiǎn)單的短信驗(yàn)證功能
我這里只是編寫一個(gè)簡(jiǎn)單的短信驗(yàn)證功能,要是用其他的語音驗(yàn)證。。。。等等需要去秒滴云官方下載文檔,下面是編寫的一個(gè)config文檔,專門存放一些參數(shù)

Part4編寫http請(qǐng)求工具類
public class HttpUtil
{
/**
* 構(gòu)造通用參數(shù)timestamp、sig和respDataType
*
* @return
*/
public static String createCommonParam()
{
// 時(shí)間戳
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;
}
/**
* post請(qǐng)求
*
* @param url
* 功能和操作
* @param body
* 要post的數(shù)據(jù)
* @return
* @throws IOException
*/
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();
// 設(shè)置連接參數(shù)
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(5000);
conn.setReadTimeout(20000);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 提交數(shù)據(jù)
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(body);
out.flush();
// 讀取返回?cái)?shù)據(jù)
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;
}
/**
* 回調(diào)測(cè)試工具方法
*
* @param url
* @param reqStr
* @return
*/
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();
// 設(shè)置連接參數(shù)
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(5000);
conn.setReadTimeout(20000);
// 提交數(shù)據(jù)
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(body);
out.flush();
// 讀取返回?cái)?shù)據(jù)
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;
}
}Part5生成四位數(shù)的方法
public static String runNumber() {
String str="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuilder sb=new StringBuilder(4);
for(int i=0;i<4;i++)
{
char ch=str.charAt(new Random().nextInt(str.length()));
sb.append(ch);
}
System.out.println(sb.toString());
String code = sb.toString();
return code;
}
4、執(zhí)行方法execute(),便會(huì)發(fā)送成功
public class IndustrySMS
{
private static String operation = "/industrySMS/sendSMS";
private static String accountSid = Config.ACCOUNT_SID;
private static String to = "15342349382";
private static String smsContent = "【小陶科技】登錄驗(yàn)證碼:{"+runNumber().toString()+"},如非本人操作,請(qǐng)忽略此短信。";
/**
* 驗(yàn)證碼通知短信
*/
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();
// 提交請(qǐng)求
String result = HttpUtil.post(url, body);
System.out.println("result:" + System.lineSeparator() + result);
}以上就是短信驗(yàn)證碼登錄流程詳細(xì)步驟
作者 | classabcd
怎么接私活?這個(gè)渠道你100%有用!請(qǐng)收藏!
喜歡文章,點(diǎn)個(gè)在看
評(píng)論
圖片
表情


