java 实现注册调用短信验证码(网建SMS短信通)
一、首先,我们需要在 网建SMS短信通 上注册自己的帐号
二、登录成功之后我们可以在首页看到用户名以及短信剩余数量等信息
三、下载相关的jar包
commons-logging-1.1.1.jar
commons-httpclient-3.1.jar
commons-codec-1.4.jar
四、然后来到eclipse或者idea上建立一个java项目,建相应的包结构,导入下载好的jar包,编写短信接口的工具类
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 * @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 * @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;}}
}
五、编写测试类进行测试
短信密钥:在 网建SMS短信通 用户首页点击左侧 -修改短信密钥- 可查看
public class test {//填写SMS用户首页上的用户名private static String Uid = "****";//短信密钥private static String Key = "****";//手机号码,多个号码如13800000000,13800000001,13800000002private static String smsMob = "13800000000";//短信内容private static String smsText = "【****有限公司】验证码:8888";public static void main(String[] args) {HttpClientUtil client = HttpClientUtil.getInstance();//UTF发送int result = client.sendMsgUtf8(Uid, Key, smsText, smsMob);if(result>0){System.out.println("UTF8成功发送条数=="+result);}else{System.out.println(client.getErrorMsg(result));}}
}
注:短信内容必须包含【中文公司名称】或【中文网站名称】,否则控制台会输出“短信签名格式不正确”。
java 实现注册调用短信验证码(网建SMS短信通)相关推荐
- 项目中发送短信--中国网建SMS短信通
中国网建SMS短信通官网 http://sms.webchinese.com.cn API:http://sms.webchinese.com.cn/api.shtml 官网API已经说得很详细了,这 ...
- 中国网建SMS短信接口调用(java发送和接收手机短信)
引言 最近有点想写博客的冲动,不为别的,只为能为那些和我一样碰到困难的码农们尽一份绵薄之力,分享技术和心得,探讨疑点,共同学习,共同进步.但提笔时又不知从何作起,java这门语言犹如一部浩瀚的生活字典 ...
- Java利用中国网建SMS短信通平台发送手机短信
JAVA发送手机短信,流传有几种方法: (1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册; (2)使用短信mao的方式进行短信的 ...
- 中国网建 SMS短信接口帮助文档
中国网建 SMS短信接口帮助文档 1.登录中国网建官网(网址:http://www.smschinese.cn/) 2.进行网站中的测试案例 3.可以看到的验证码已经发送了回来 4.官网的API接口帮 ...
- C# 短信通知(中国网建sms短信平台API)
使用第三方短信平台(中国网建SMS短信平台)进行短信通知 http://www.smschinese.cn/api.shtml string str = http://utf8.api.smschin ...
- 中国网建SMS短信接口调用(java发送短信)
java发送短信 package sms;import org.apache.commons.httpclient.Header; import org.apache.commons.httpclie ...
- JAVA接入中国网建SMS短息短信平台开发(学习整理)
1.增加架包 <!-- 中国网建提供的SMS短信 --><dependency><groupId>commons-httpclient</groupId> ...
- 中国网建SMS短信通API下行接口参数
GBK编码发送接口地址: http://gbk.sms.webchinese.cn/?Uid=本站用户名&Key=接口安全密码&smsMob=手机号码&smsText=短信内容 ...
- java短信验证码失效时间_Java实现短信验证码--设置发送间隔时间,以及有效时间(Java+Redis)...
Java实现短信验证码--设置发送间隔时间,以及有效时间(Java+Redis) 这篇文章,实现了Java发送手机短信验证码发送的间隔时间,以及手机验证码的有效时间和手机验证码格式的合法性验证,可以防 ...
最新文章
- 《软件加密与解密》第三版学习日志一
- 服务器上出现应用程序错误。此应用程序的当前自定义错误设置禁止远程查看应用程序_手把手教你使用Qlik(一):数据关联并创建应用程序...
- 文章和随笔的标题好像没有HtmlEnCode。
- Redis单机和集群环境搭建
- 基于ASP.NET Core的模块化设计: 虚拟文件系统
- ftp 服务器 性能,FTP 服务器性能 测试点
- 6系A卡笔记本移动版:HD6470/6550/6630/6650/6730/6770M
- 控制台应用程序中Main函数的args参数
- Paip.声明式编程以及DSL 总结
- 浙大PAT 1058
- html图片平移效果,怎么用CSS移动图片?
- 海水淡化除硼工艺及方法
- Overload vs Override
- 以下不是python语言合法变量_违法行为的客体是指法律所保护的而为违法行为所侵害的:()...
- 微信引流专家 v1.1.2.1
- vue 移动端适配flexible.js使用方法
- 为什么说 Flutter 可能不是下一件大事?
- html 让其中一个div浮在另一个div上面
- 钢筋铁骨的我,还是被各种奇葩渣公司打得措手不及
- 小技巧---ubuntu下截图,截图全屏Prt Sc SysRq,截图当前窗口Alt+Prt Sc SysRq
热门文章
- [Go]字符串转int64数值型
- Windows Defender Advanced Threat Protection
- 建筑诊断用热像仪应用-flir T530红外热成像仪
- 为什么打印文档一张却多打出两张、三张、多张
- 汽车行业竞争激烈下,零跑汽车股价上涨也是够狠的
- 客快物流大数据项目(六十六):车辆主题
- java华容道教程_Java初学之华容道游戏
- kubernetes强制删除namespace
- 为什么有些共同生活在一起很多年的夫妻,遇到男方破产或者有负债的情况时女方会离开男方?...
- js的手机号码和固定电话号码同时验证(在同一个text内)