短信验证登陆-中国网建提供的SMS短信平台

时间:2023-03-08 21:15:47

一、JAVA发送手机短信常见的有三种方式(如下所列):

  使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册

  使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备。

  使用中国网建提供的SMS短信平台(申请账号地址:http://sms.webchinese.cn/default.shtml

二、一下整理了SMS短信验证过程:

1、首先需要带入三个包:

commons-httpclient-3.1.jar      commons-logging-1.0.4.jar      codec-1.3.jar

2、创建发送短信的类(一般写在工具类中),其中要通过短信内容要进行设置编码集为utf-8,调用第三方接口传参要按照第三方文档规范:

package com.demo.util;
import java.io.IOException;
import java.util.Map; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod; public class HttpRequestUtil { /**
* HttpClient 模拟POST请求
*/
public static String postRequest(String url, Map<String, String> params) {
//构造HttpClient的实例
HttpClient httpClient = new HttpClient(); //创建POST方法的实例
PostMethod postMethod = new PostMethod(url); //设置请求头信息
postMethod.setRequestHeader("Connection", "close"); //添加参数
for (Map.Entry<String, String> entry : params.entrySet()) {
postMethod.addParameter(entry.getKey(), entry.getValue());
} //使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次
httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false); //接收处理结果
String result = null;
try {
//执行Http Post请求
httpClient.executeMethod(postMethod); //返回处理结果
result = postMethod.getResponseBodyAsString();
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("请检查输入的URL!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
System.out.println("发生网络异常!");
e.printStackTrace();
} finally {
//释放链接
postMethod.releaseConnection(); //关闭HttpClient实例
if (httpClient != null) {
((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
httpClient = null;
}
}
return result;
} /**
* HttpClient 模拟GET请求
*/
public static String getRequest(String url, Map<String, String> params) {
//构造HttpClient实例
HttpClient client = new HttpClient(); //拼接参数
String paramStr = "";
for (String key : params.keySet()) {
paramStr = paramStr + "&" + key + "=" + params.get(key);
}
paramStr = paramStr.substring(1); //创建GET方法的实例
GetMethod method = new GetMethod(url + "?" + paramStr); //接收返回结果
String result = null;
try {
//执行HTTP GET方法请求
client.executeMethod(method); //返回处理结果
result = method.getResponseBodyAsString();
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("请检查输入的URL!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
System.out.println("发生网络异常!");
e.printStackTrace();
} finally {
//释放链接
method.releaseConnection(); //关闭HttpClient实例
if (client != null) {
((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
client = null;
}
}
return result;
}
}
package com.demo.util;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class SendMsgUtil {
/**
* 发送短信消息
*/
@SuppressWarnings("deprecation")
public static String sendMsg(String phones,String content){
//短信接口URL提交地址utf-8
String url = "http://utf8.sms.webchinese.cn"; Map<String, String> params = new HashMap<String, String>();
//用户名,例:abc123456
params.put("Uid", "yu3961520");
//短信接口密钥 ,不是密码,例:79c3053154d313a34567
params.put("Key", "d41d8cd98f00b204e980"); //手机号码,多个号码使用英文逗号进行分割 params.put("smsMob", phones);
//将短信内容进行URLEncoder编码
params.put("smsText", URLEncoder.encode(content)); return HttpRequestUtil.getRequest(url, params);
} /**
* 随机生成6位随机验证码
*/
public static String createRandomVcode(){
//验证码
String vcode = "";
for (int i = 0; i < 6; i++) {
vcode = vcode + (int)(Math.random() * 9);
}
return vcode;
} /**
* 测试
*/
public static void main(String[] args) {
//多个手机号请用半角,隔开
//例:System.out.println(sendMsg("18912345678,17812345678", "尊敬的用户,您的验证码为" + SendMsgUtil.createRandomVcode() + ",有效期为60秒,如有疑虑请详询400-3023-4493(客服电话)【中国联通】"));
System.out.println(sendMsg("电话号码", "*****,你的验证码为" + SendMsgUtil.createRandomVcode() + ",有效期为60秒,如有疑虑请详询(******)【签名,这是验证格式的必须填】"));
}
}

3、短信发送后返回值 说明 :
-1 没有该用户账户 
-2 密钥不正确(不是用户密码) 
-3 短信数量不足 
-11 该用户被禁用 
-14 短信内容出现非法字符 
-41 手机号码为空 
-42 短信内容为空 
大于0 短信发送数量