|
调了好几天短信验证测试,终于成功,现将全部源代码发给大家,希望对各位有所帮助,少走弯路。 如有问题,请加入我的QQ :893586170
Model.prototype.saveUserBtnClick = function(event) {
var userData = this.comp("userData");
var params = {
"userData" : userData.toJson(true)
};
var success = function(resultData) {
userData.applyUpdates();
justep.Util.hint("用户信息保存成功");
};
Baas.sendRequest({
"url" : "/takeout",
"action" : "save",
"params" : params,
"success" : success
});
var paramsa = {
"fPhoneNumber" : userData.val("fPhoneNumber")
};
var successa = function(resultData)
{
//拿到从后台返回的验证码
alert(resultData.CheckCode);
};
Baas.sendRequest({
"url" : "/takeout",
"action" : "DuanXinCheck",
"params" : paramsa,
"success" : successa
});
};
private static void DuanXinCheck(ServletRequest request, ServletResponse response) throws SQLException, IOException, NamingException , Exception
{
JSONObject params = (JSONObject) JSONObject.parse(request.getParameter("params"));
String reta =null;
String fPhoneNumber = params.getString("fPhoneNumber");
//生成6位数字的随机字符串
int suiji = new Random().nextInt(899999)+100000;
String backStr=Integer.toString(suiji);
// 将随机字符串通过jsonOBJ返回前台
JSONObject jsonObj = new JSONObject();
jsonObj.put("CheckCode", backStr);
Util.writeJsonToResponse(response, jsonObj);
String dxurl="http://utf8.sms.webchinese.cn";
String PostData ="Uid=kljkljklk1&Key=ekjlkj271c32a&smsMob="+fPhoneNumber+"&smsText=验证码:"+backStr+" (本信息由系统自动发出,不要回复)";
reta = Send.SMS(PostData,dxurl);
}
package com.justep.baas.takeout;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/**
*
* @author Administrator
*/
public class Send {
public static String SMS(String postData, String postUrl) {
try {
//发送POST请求
URL url = new URL(postUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", "" + postData.length());
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(postData);
out.flush();
out.close();
//获取响应状态
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println("connect failed!");
return "";
}
//获取响应内容体
String line, result = "";
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
while ((line = in.readLine()) != null) {
result += line + "\n";
}
in.close();
return result;
} catch (IOException e) {
e.printStackTrace(System.out);
}
return "";
}
}
|
|