java调用发送短信API
来源:互联网 发布:好喝的洋酒推荐 知乎 编辑:程序博客网 时间:2024/04/25 08:19
需要用到的jar包
工具类和测试类
工具类:
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; } /** * @author wangzs * @Description 发送 post请求 * @date 2017-11-6 * @time 下午4:36:34 * @param httpUrl 地址 * @return */ public String sendHttpPost(String httpUrl) { HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost return sendHttpPost(httpPost,"utf-8"); } /** * @author wangzs * @Description 发送 post请求 * @date 2017-11-6 * @time 下午4:36:58 * @param httpUrl 地址 * @param maps 参数 * @param type 字符编码格式 * @return */ 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); } /** * @author wangzs * @Description 发送Post请求 * @date 2017-11-6 * @time 下午4:38:18 * @param httpPost * @param reponseType * @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; } /** * @author wangzs * @Description 发送 get请求 * @date 2017-11-6 * @time 下午4:38:39 * @param httpUrl * @return */ public String sendHttpGet(String httpUrl) { HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求 return sendHttpGet(httpGet); } /** * @author wangzs * @Description 发送 get请求Https * @date 2017-11-6 * @time 下午4:38:52 * @param httpUrl * @return */ public String sendHttpsGet(String httpUrl) { HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求 return sendHttpsGet(httpGet); } /** * @author wangzs * @Description TODO(发送utf8) * @date 2017-11-6 * @time 下午4:39:16 * @param Uid * @param Key * @param content * @param mobiles * @return */ @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); } /** * @author wangzs * @Description TODO(发送utf8) * @date 2017-11-6 * @time 下午4:39:58 * @param Uid * @param Key * @param content * @param mobiles * @return */ @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); } /** * @author wangzs * @Description 发送Get请求 * @date 2017-11-6 * @time 下午4:40:28 * @param httpGet * @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; } /** * @author wangzs * @Description 发送Get请求Https * @date 2017-11-6 * @time 下午4:40:44 * @param httpGet * @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; } /** * @author wangzs * @Description TODO(返回异常原因) * @date 2017-11-6 * @time 下午4:40:57 * @param errorCode * @return */ 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; } }}
测试类:
public class test { //用户名和秘钥都需要到中国网建提供的SMS短信平台上查看 //用户名 private static String Uid = "WangZongShi"; //接口安全秘钥 private static String Key = "3d99cc5wqeffed58"; //手机号码,多个号码如13800000000,13800000001,13800000002 private static String smsMob = "18888888888"; //短信内容 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)); } }}
相关链接:中国网建提供的SMS短信平台(申请账号地址:http://sms.webchinese.cn/default.shtml);
会出现的问题:
请各位大神发现并提出。 1. 2. 3. 。 。 。
谢谢观看~
阅读全文
0 0
- java调用发送短信API
- java 调用鹏讯通短信平台api发送短信
- java调用短信api接口发送短信demo实例
- java调用短信api发送短信进行注册
- java 调用云通信的api发送短信
- 调用Android系统API发送短信,打电话
- 调用中国网建API发送短信
- 阿里云短信发送API接口调用
- 程序调用飞信API发送免费短信(JAVA例子,其他语言一样用)
- 程序调用飞信API发送免费短信(JAVA例子,其他语言一样用)
- 程序调用飞信API发送免费短信(JAVA例子,其他语言一样用)
- 阿里-短信发送API(SendSms)---JAVA
- JAVA利用短信平台API免费发送短信的程序
- Java 调用smslib发送接收短信
- Java调用SMSLib发送短信详解
- java调用webservice接口发送短信
- 调用Java API发送传真
- java调用C# webservice短信猫发送短信
- 从驱动层到应用层打通Android系统通路
- [高级软件工程实验]将menu设计为可重用的子系统
- Maven学习总结(四)——Maven核心概念
- 已经把用户加入MQ的执行用户组了,为什么该用户还是没有权限执行MQ
- PTA 爆内存函数实例
- java调用发送短信API
- itext pdf加水印
- 集成的HTTP嗅探器HttpWatch更新至v11.0.21,改进F5刷新机制
- Python with
- 算法导论学习-插入排序python实现
- spring mvc + mybatis 连接同一地址的多个数据库
- menuconfig详解(1)——Enable loadable module support
- mybatis动态sql中foreach标签的使用
- js function中返回function 闭包