java调用发送短信API
2017-11-06 17:01
357 查看
需要用到的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. 。 。 。
谢谢观看~
相关文章推荐
- java 调用云通信的api发送短信
- 程序调用飞信API发送免费短信(JAVA例子,其他语言一样用)
- 程序调用飞信API发送免费短信(JAVA例子,其他语言一样用)
- 程序调用飞信API发送免费短信(JAVA例子,其他语言一样用)
- java调用短信api发送短信进行注册
- Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用
- Java调用WebService接口实现发送手机短信验证码功能
- 阿里云短信发送API(SendSms)---JAVA
- JAVA后台通过http调用短信API
- Cocos2d-x3.3RC0通过JNI调用Android的Java层URI代码发送短信
- JAVA利用短信平台API免费发送短信的程序
- Java调用SMSLib发送短信详解
- Java阿里云通信短信服务发送短信调用
- Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用
- 使用JAVA实现模拟登陆并发送新浪微博(非调用新浪API)
- java调接口实现发送手机短信验证码功能,手机验证码,接口调用
- 中国网建SMS短信接口调用(java发送和接收手机短信)
- Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用
- 调用阿里云短信服务平台API实现ERP系统短信发送功能
- java 调用 短信API服务 (聚合数据平台)