java後臺獲取簡訊驗證碼
第三方簡訊平臺工具類
package com.doubi.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WebSendSmsClient {
/**
*
* @param 簡訊通客戶介面測試
* @param sendsmsaddress
* @return
*
*/
public String commandID = "3";
public String username = "-------"; //客戶名
public String password = "******"; //密碼
public String serviceURL = "http://124.173.70.59:8081/SmsAndMms/mg";
public static String connectURL(String commString, String sendsmsaddress) {
String rec_string = "";
URL url = null;
HttpURLConnection urlConn = null;
try {
url = new URL(sendsmsaddress); // 根據資料的傳送地址構建URL
urlConn = (HttpURLConnection) url.openConnection(); // 開啟連結
urlConn.setConnectTimeout(30000); // 連結超時設定為30秒
urlConn.setReadTimeout(30000); // 讀取超時設定30秒
urlConn.setRequestMethod("POST"); // 連結相應方式為post
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
OutputStream out = urlConn.getOutputStream();
out.write(commString.getBytes());
// out.write(URLEncoder.encode(commString, "UTF-8").getBytes());
out.flush();
out.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "UTF-8"));
StringBuffer sb = new StringBuffer();
int ch;
while ((ch = rd.read()) > -1) {
sb.append((char) ch);
}
rec_string = sb.toString().trim();
rec_string = URLDecoder.decode(rec_string, "UTF-8");
rd.close();
} catch (Exception e) {
rec_string = "-107";
} finally {
if (urlConn != null) {
urlConn.disconnect();
}
}
return rec_string;
}
public String sendSms(String mobile, String content) {
String res = "";
try {
String commString = "Sn=" + username + "&Pwd=" + password + "&mobile=" + mobile + "&content=" + content;
res = connectURL(commString, serviceURL);
} catch (Exception e) {
return "-10000";
}
// 設定返回值 解析返回值
String resultok = "";
// //正則表示式
Pattern pattern = Pattern.compile("<int xmlns=\"http://tempuri.org/\">(.*)</int>");
Matcher matcher = pattern.matcher(res);
while (matcher.find()) {
resultok = matcher.group(1);
}
return resultok;
}
public static void main(String[] args) throws Exception {
String mobile = "***********";
String msContent = String.valueOf((int) (Math.random() * 1000000)) + "的";
byte[] b = msContent.getBytes("UTF-8");
String t = new String(b, "UTF-8");
String content = new String(t.getBytes("UTF-8"));
WebSendSmsClient tt = new WebSendSmsClient();
System.out.println("--------");
System.out.println(content);
String res = tt.sendSms(mobile, content);
// 設定返回值
System.out.println(res);
}
}
前臺呼叫頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../jquery-1.11.3/jquery.js"></script>
<script type="text/javascript" src="../jquery-1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var text;
$("#getCode").click(function() {
var phone = $("#phone").val();
if(phone==""){
alert("手機號不能為空");
return;
}
text = $.ajax({
url : '<%=request.getContextPath()%>/rest/user/getPhoneCode/' + phone,
type : 'GET',
datatType : 'json',
async : false,
success:function(){
$("#result").text("success");
}
}).responseText;
});
$("#changePassword").click(function(){
var code = $("#code").val();
var surePassword= $("#surePassword").val();
var newPassword = $("#newPassword").val();
var userid = $("#userid").val();
if(code==""){
alert("驗證碼不能為空");
return;
}
if(code!=text){
alert("驗證碼輸入錯誤");
return;
}
if(newPassword==""){
alert("密碼不能為空");
return;
}
if(surePassword==""){
alert("確認密碼不能為空");
return;
}
if(surePassword!=newPassword){
alert("密碼與確認密碼不一致");
return;
}
alert(code+"-"+newPassword+"-"+userid);
var t = $.ajax({
url:'<%=request.getContextPath()%>/rest/user/updatePasswordByCode?userid='+userid+'&code='+code+'&newPassword='+newPassword,
type : 'POST',
datatType : 'json',
async : false
});
alert(t);
});
});
</script>
</head>
<body>
手機號:
<input type="text" name="phone" id="phone" />
<button id="getCode">獲取驗證碼</button><p name="result" id="result"><font color="red"></font></p><br>
<input type="text" name="code" id="code"><br>
使用者id:<input type="text" name="userid" id="userid"><br>
請輸入新密碼:<input type="text" name="newPassword" id="newPassword"/><br>
確認新密碼:<input type="text" name="surePassword" id="surePassword"/><br>
<button id="changePassword">修改</button>
</body>
</html>