短信验证

Posted juddy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了短信验证相关的知识,希望对你有一定的参考价值。

在我们开发一套新的系统中,现在短信验证是非常常见的,比如修改密码,发送验证码等等,今天就对于简单的短信验证进行一个说明

首先了解一下短信验证的一个原理:我们通过图示的方式进行了解

技术图片

 

 由上面的图示可以看出,在短信发送过程中并不是直接发送给用户的,而是通过短信运营商来进行实现的,

现在短信运营商有很多:今天我们以中国网建为例

下面是对中国网建进行的一系列操作:

使用中国网建作为短信运营商的的操作流程

1.进行注册

2.通过用户名和密码进行登录

3.设置签名(尽量不要设置大型公司的名称)

4.编辑短信内容

5.设置要发送给那个手机号码

6.整合到代码中点击短信API接口

7.进行示例程序的下载(里面会包括三个jar包)

8.对里面包含的.txt文件进行查看,了解其里面所定义的变量的含义

9.修改短信安全秘钥(在中国网建登录成功会左侧边栏就可以看到)

前面1-6步骤直接搜索中国网建就可以进行相关的操作

后面7-9步骤实在代码中需要修改的地方,详细了解请查看下面的代码

第三步:进行一个实践

首先将步骤7下载下来的示例程序解压,并将自己需要的导入进自己使用的开发工具中,在这以eclipse为例

导入后的的效果:

技术图片

 

 下面是详细代码:

这是一个工具类:
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


public class HttpClientUtil { 
private RequestConfig requestConfig = RequestConfig.custom() 
.setSocketTimeout(15000) 
.setConnectTimeout(15000) 
.setConnectionRequestTimeout(15000) 
.build(); 

private static HttpClientUtil instance = null; 
private HttpClientUtil(){} 
public static HttpClientUtil getInstance(){ 
if (instance == null) { 
instance = new HttpClientUtil(); 

return instance; 


/** 
* 发送 post请求 
* @param httpUrl 地址 
*/ 
public String sendHttpPost(String httpUrl) { 
HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost 
return sendHttpPost(httpPost,"utf-8"); 



/** 
* 发送 post请求 
* @param httpUrl 地址 
* @param maps 参数 
* @param type 字符编码格式 
*/ 
public String sendHttpPost(String httpUrl, Map<String, String> maps,String type) { 
HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost 
// 创建参数队列 
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
for (String key : maps.keySet()) { 
nameValuePairs.add(new BasicNameValuePair(key, maps.get(key))); 

try { 
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, type)); 
} catch (Exception e) { 
e.printStackTrace(); 

return sendHttpPost(httpPost,type); 


/** 
* 发送Post请求 
* @param httpPost 
* @return 
*/ 
private String sendHttpPost(HttpPost httpPost,String reponseType) { 
CloseableHttpClient httpClient = null; 
CloseableHttpResponse response = null; 
HttpEntity entity = null; 
String responseContent = null; 
try { 
// 创建默认的httpClient实例. 
httpClient = HttpClients.createDefault(); 
httpPost.setConfig(requestConfig); 
// 执行请求 
response = httpClient.execute(httpPost); 
entity = response.getEntity(); 
responseContent = EntityUtils.toString(entity, reponseType); 
} catch (Exception e) { 
e.printStackTrace(); 
} finally { 
try { 
// 关闭连接,释放资源 
if (response != null) { 
response.close(); 

if (httpClient != null) { 
httpClient.close(); 

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


return responseContent; 


/** 
* 发送 get请求 
* @param httpUrl 
*/ 
public String sendHttpGet(String httpUrl) { 
HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求 
return sendHttpGet(httpGet); 


/** 
* 发送 get请求Https 
* @param httpUrl 
*/ 
public String sendHttpsGet(String httpUrl) { 
HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求 
return sendHttpsGet(httpGet); 


/**
* @Title: sendMsgUtf8
* @Description: TODO(发送utf8)
* @param: @param Uid
* @param: @param Key
* @param: @param content
* @param: @param mobiles
* @param: @return 
* @date: 2017-3-22 下午5:58:07
* @throws
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public int sendMsgUtf8(String Uid,String Key,String content,String mobiles){
Map maps = new HashMap();
maps.put("Uid", Uid);
maps.put("Key", Key);
maps.put("smsMob", mobiles);
maps.put("smsText", content);
String result = sendHttpPost("http://utf8.sms.webchinese.cn", maps, "utf-8");
return Integer.parseInt(result);
}

/**
* @Title: sendMsgUtf8
* @Description: TODO(发送utf8)
* @param: @param Uid
* @param: @param Key
* @param: @param content
* @param: @param mobiles
* @param: @return 
* @return: int 
* @author: ly
* @date: 2017-3-22 下午5:58:07
* @throws
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public int sendMsgGbk(String Uid,String Key,String content,String mobiles){
Map maps = new HashMap();
maps.put("Uid", Uid);
maps.put("Key", Key);
maps.put("smsMob", mobiles);
maps.put("smsText", content);
String result = sendHttpPost("http://gbk.sms.webchinese.cn", maps, "gbk");
return Integer.parseInt(result);
}

/** 
* 发送Get请求 
* @param httpPost 
* @return 
*/ 
private String sendHttpGet(HttpGet httpGet) { 
CloseableHttpClient httpClient = null; 
CloseableHttpResponse response = null; 
HttpEntity entity = null; 
String responseContent = null; 
try { 
// 创建默认的httpClient实例. 
httpClient = HttpClients.createDefault(); 
httpGet.setConfig(requestConfig); 
// 执行请求 
response = httpClient.execute(httpGet); 
entity = response.getEntity(); 
responseContent = EntityUtils.toString(entity, "UTF-8"); 
} catch (Exception e) { 
e.printStackTrace(); 
} finally { 
try { 
// 关闭连接,释放资源 
if (response != null) { 
response.close(); 

if (httpClient != null) { 
httpClient.close(); 

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


return responseContent; 


/** 
* 发送Get请求Https 
* @param httpPost 
* @return 
*/ 
private String sendHttpsGet(HttpGet httpGet) { 
CloseableHttpClient httpClient = null; 
CloseableHttpResponse response = null; 
HttpEntity entity = null; 
String responseContent = null; 
try { 
// 创建默认的httpClient实例. 
PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString())); 
DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher); 
httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build(); 
httpGet.setConfig(requestConfig); 
// 执行请求 
response = httpClient.execute(httpGet); 
entity = response.getEntity(); 
responseContent = EntityUtils.toString(entity, "UTF-8"); 
} catch (Exception e) { 
e.printStackTrace(); 
} finally { 
try { 
// 关闭连接,释放资源 
if (response != null) { 
response.close(); 

if (httpClient != null) { 
httpClient.close(); 

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


return responseContent; 


/**
* @Title: getErrorMsg
* @Description: TODO(返回异常原因)
* @param: @param errorCode
*/
public String getErrorMsg(int errorCode){
if(errorCode==-1){
return "没有该用户账户";
}else if(errorCode==-2){
return "接口密钥不正确";
}else if(errorCode==-3){
return "短信数量不足";
}else if(errorCode==-4){
return "手机号格式不正确";
}else if(errorCode==-21){
return "MD5接口密钥加密不正确";
}else if(errorCode==-11){
return "该用户被禁用";
}else if(errorCode==-14){
return "短信内容出现非法字符";
}else if(errorCode==-41){
return "手机号码为空";
}else if(errorCode==-42){
return "短信内容为空";
}else if(errorCode==-51){
return "短信签名格式不正确";
}else if(errorCode==-6){
return "IP限制";
}else{
return "未知错误码:"+errorCode;
}
}
}

下面是一个测试类:

import java.util.HashMap;
import java.util.Map;

/** 
* @Title: http://www.smschinese.cn/api.shtml
* @date 2011-3-22
* @version V1.2 
*/
public class test {

//用户名,是注册是自己所填的用户名
private static String Uid = "来自地狱的勇士";

//接口安全秘钥,当你注册成功并登陆后,会有以下修改短信秘钥的选项,点击进去进行赋值
private static String Key = "d41d8cd98f00b204e980";

//手机号码,多个号码如13800000000,13800000001,13800000002,这个电话号码填的是你要发送短信的用户的电话号码
private static String smsMob = "***************";

//短信内容
private static String smsText = "验证码:8888";


public static void main(String[] args) {

HttpClientUtil client = HttpClientUtil.getInstance();
//随机生成一个验证码
int num=(int)(Math.random()*10000);
smsText=num+"";
//UTF发送
int result = client.sendMsgUtf8(Uid, Key, smsText, smsMob);
if(result>0){
System.out.println("UTF8成功发送条数=="+result);
}else{
System.out.println(client.getErrorMsg(result));
}
}
}

最终出现的结果请参考下表:

短信发送后返回值说 明
-1 没有该用户账户
-2 接口密钥不正确 [查看密钥]
不是账户登陆密码
-21 MD5接口密钥加密不正确
-3 短信数量不足
-11 该用户被禁用
-14 短信内容出现非法字符
-4 手机号格式不正确
-41 手机号码为空
-42 短信内容为空
-51 短信签名格式不正确
接口签名格式为:【签名内容】
-52 短信签名太长
建议签名10个字符以内
-6 IP限制
大于0 短信发送数量

以上就是对于整个短信发送验证的全部内容

欢迎大家批评,指正 

 

以上是关于短信验证的主要内容,如果未能解决你的问题,请参考以下文章

为啥注册的时候没有发送短信验证码?

手机收不到短信收不到验证码无法取件

QQ加群需要短信验证怎么解除?

Vue实战项目之获取手机短信验证码

各大APP注册时发送短信验证码是怎么实现的?

验证码---短信验证码