1. 程式人生 > >java對接短信驗證碼功能,短信驗證碼開發

java對接短信驗證碼功能,短信驗證碼開發

ack eth httpurl tex -a 公司 ice 尋找 count

今天公司提出一個需求,要在現有項目上收集註冊用戶的真實手機號,由於之前沒有接觸過這一塊,只能尋求度娘的幫助,經過一天的努力,終於完成了,現整理記錄下已備查閱。

1 解決方案:在註冊時要求用戶進行手機驗證。
2 尋找短信供應商:由於對這一塊不是太懂,大學同學推薦一家他們公司在用的給我。
3 代碼實現
首先到互億無線短信平臺註冊一個帳號,並登錄到用戶中心,選驗證碼模塊下載接口文檔,文檔下載下來是一個壓縮包,我們項目是用java的jsp開發的,直接找到目錄DEMO/JSP

提示:開始之前先看一下官方對接說明文檔

第一步 把前端html代碼整合到註冊頁面
html 代碼:

<form action="reg.jsp" method="post" name="formUser" onSubmit="return register();">

<table width="100%" border="0" align="left" cellpadding="5" cellspacing="3">
<tr>
<td align="right">手機<td>
<input id="mobile" name="mobile" type="text" size="25" class="inputBg" /><span style="color:#FF0000"> *</span>
</tr>
<tr>
<td align="right">驗證碼</td>
<td>
<input type="text" name="gd_code" class="inputBg" size="25" id="gd_code" onkeyup="value=value.replace(/[^\d]/g,‘‘)" placeholder="請輸入正確的驗證碼">
<span> <img src="code.jsp" onClick="javascript:this.src=this.src+‘?date=‘+Date();" ></span>
</td>
</tr>
<tr>
<td align="right">手機驗證碼</td>
<td align="left">
<input type="text" name="mobile_code" class="inputBg" size="25" />
<input id="zphone" type="button" value=" 獲取手機驗證碼 " style="width: 120px" onClick="get_mobile_code()">
</td>
</tr>
</table>
</form>

javascript代碼

<script language="javascript">
function get_mobile_code(){
$.post(‘sms.jsp‘, {mobile:jQuery.trim($(‘#mobile‘).val()),send_code:$("#gd_code").val()}, function(msg) {
alert(jQuery.trim(unescape(msg)));
if(msg==‘提交成功‘){
RemainTime();
}else{
location.reload();
}
});
};
var iTime = 59;
var Account;
function RemainTime(){
document.getElementById(‘zphone‘).disabled = true;
var iSecond,sSecond="",sTime="";
if (iTime >= 0){
iSecond = parseInt(iTime%60);
iMinute = parseInt(iTime/60)
if (iSecond >= 0){
if(iMinute>0){
sSecond = iMinute + "分" + iSecond + "秒";
}else{
sSecond = iSecond + "秒";
}
}
sTime=sSecond;
if(iTime==0){
clearTimeout(Account);
sTime=‘獲取手機驗證碼‘;
iTime = 59;
document.getElementById(‘zphone‘).disabled = false;
}else{
Account = setTimeout("RemainTime()",1000);
iTime=iTime-1;
}
}else{
sTime=‘沒有倒計時‘;
}
document.getElementById(‘zphone‘).value = sTime;
}
</script>

第一步 把後端jsp代碼整合到程序邏輯中。
註意:把code.jsp sms.jsp這兩個文件要上傳到項目路徑下

使用 HttpURLConnection請求短信接口就行了

String postUrl = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

int mobile_code = (int)((Math.random()9+1)100000); //獲取隨機數

String account = "用戶名"; //查看用戶名是登錄用戶中心->驗證碼短信->產品總覽->APIID
String password = "密碼"; //查看密碼請登錄用戶中心->驗證碼短信->產品總覽->APIKEY
String mobile = request.getParameter("mobile");
String content = new String("您的驗證碼是:" + mobile_code + "。請不要把驗證碼泄露給其他人。");
String send_code = request.getParameter("send_code");
String session_code = (String)session.getAttribute("randStr");

// 此處驗證碼僅限於純數字比對
if (Integer.parseInt(send_code) != Integer.valueOf(session_code)) {
String message = new String("請輸入正確的驗證碼");
out.println(message);
return;
}

try {

URL url = new URL(postUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);//允許連接提交信息
connection.setRequestMethod("POST");//網頁提交方式“GET”、“POST”
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Connection", "Keep-Alive");
StringBuffer sb = new StringBuffer();
sb.append("account="+account);
sb.append("&password="+password);
sb.append("&mobile="+mobile);
sb.append("&content="+content);
OutputStream os = connection.getOutputStream();
os.write(sb.toString().getBytes());
os.close();

String line, result = "";
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
while ((line = in.readLine()) != null) {
    result += line + "\n";
}
in.close();
out.println(result);

} catch (IOException e) {
e.printStackTrace(System.out);
}

java對接短信驗證碼功能,短信驗證碼開發