|
发表于 2015-4-26 13:26:37
|
显示全部楼层
调试短信码程序,但是不能发手机发短信,
//前台
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 {
// 参数序列化
JSONObject params = (JSONObject) JSONObject.parse(request.getParameter("params"));
String fPhoneNumber = params.getString("fPhoneNumber");
System.out.println("bbbbbbbbbbb="+fPhoneNumber);
//生成6位数字的随机字符串
int suiji = new Random().nextInt(899999)+100000;
String backStr=Integer.toString(suiji);
// 将随机字符串通过jsonOBJ返回前台
JSONObject jsonObj = new JSONObject();
jsonObj.put("CheckCode", backStr);
System.out.println("jhhjhkkkkllj="+backStr);
Util.writeJsonToResponse(response, jsonObj);
//把这个随机数发到短信平台de
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn");
//在头文件中设置转码
post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");
NameValuePair[] data ={ new NameValuePair("Uid", "wky1232001"),new NameValuePair("Key", "e348c6e239cee271c32a"),new NameValuePair("smsMob",fPhoneNumber),new NameValuePair("smsText","验证码:"+backStr+" (本信息由系统自动发出,不要回复)")};
//这段Java代码是直接从他家demo拷贝下来的,只需要修改这句话,填写你在他家注册的用户名、短信密钥,参数中加要发送的手机号和验证码短信
post.setRequestBody(data);
try{
client.executeMethod(post);
Header[] headers = post.getResponseHeaders();
int statusCode = post.getStatusCode();
System.out.println("statusCode:"+statusCode);
for(Header h : headers)
{
System.out.println(h.toString());
}
String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
System.out.println(result); //打印返回消息状态
}
finally{
post.releaseConnection();
}
}
|
|