优秀如你,代码世界因你绽放光彩!!!

聚合短信验证码java代码

聚合官网:https://www.juhe.cn

申请账号注册:进入官网-------》短信业务------》短信API服务------申请开通---------》个人中心--------》我的接口---------》申请数据------------》短信验证码------》点击  模板

 

 

 

 

 

 

 

 

全部通过后:::模板ID    AppKey  要用

 

 

 

官网上的demo ,没改变什么::

 IDEA::::::::::::::::::::   pom.xml 依赖

 

  1 vimport java.io.BufferedReader;
  2 import java.io.DataOutputStream;
  3 import java.io.IOException;
  4 import java.io.InputStream;
  5 import java.io.InputStreamReader;
  6 import java.io.UnsupportedEncodingException;
  7 import java.net.HttpURLConnection;
  8 import java.net.URL;
  9 import java.net.URLEncoder;
 10 import java.util.HashMap;
 11 import java.util.Map;
 12  
 13 import net.sf.json.JSONObject;
 14  
 15 /**
 16 *短信API服务调用示例代码 - 聚合数据
 17 *在线接口文档:http://www.juhe.cn/docs/54
 18 **/
 19  
 20 public class JuheDemo {
 21     public static final String DEF_CHATSET = "UTF-8";
 22     public static final int DEF_CONN_TIMEOUT = 30000;
 23     public static final int DEF_READ_TIMEOUT = 30000;
 24     public static String userAgent =  "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
 25  
 26     //配置您申请的KEY
 27     public static final String APPKEY ="*************************";
 28  
 29     //1.屏蔽词检查测
 30     public static void getRequest1(){
 31         String result =null;
 32         String url ="http://v.juhe.cn/sms/black";//请求接口地址
 33         Map params = new HashMap();//请求参数
 34             params.put("word","");//需要检测的短信内容,需要UTF8 URLENCODE
 35             params.put("key",APPKEY);//应用APPKEY(应用详细页查询)
 36  
 37         try {
 38             result =net(url, params, "GET");
 39             JSONObject object = JSONObject.fromObject(result);
 40             if(object.getInt("error_code")==0){
 41                 System.out.println(object.get("result"));
 42             }else{
 43                 System.out.println(object.get("error_code")+":"+object.get("reason"));
 44             }
 45         } catch (Exception e) {
 46             e.printStackTrace();
 47         }
 48     }
 49  
 50     //2.发送短信
 51     public static void getRequest2(){
 52         String result =null;
 53         String url ="http://v.juhe.cn/sms/send";//请求接口地址
 54         Map params = new HashMap();//请求参数
 55             params.put("mobile","你的手机号");//接收短信的手机号码
 56             params.put("tpl_id","短信模板ID");//短信模板ID,请参考个人中心短信模板设置
 57             params.put("tpl_value","#code#="+num);//变量名和变量值对。如果你的变量名或者变量值中带有#&=中的任意一个特殊符号,请先分别进行urlencode编码后再传递,<a href="http://www.juhe.cn/news/index/id/50" target="_blank">详细说明></a>
 58             params.put("key",APPKEY);//应用APPKEY(应用详细页查询)
 59             params.put("dtype","");//返回数据的格式,xml或json,默认json
 60  
 61         try {
 62             result =net(url, params, "GET");
 63             JSONObject object = JSONObject.fromObject(result);
 64             if(object.getInt("error_code")==0){
 65                 System.out.println(object.get("result"));
 66             }else{
 67                 System.out.println(object.get("error_code")+":"+object.get("reason"));
 68             }
 69         } catch (Exception e) {
 70             e.printStackTrace();
 71         }
 72     }
 73  
 74  
 75  
 76     public static void main(String[] args) {
 77 
 78 // 执行代码  运行就行了
 79          类名.mobileQuery();
 80  
 81     }
 82  
 83     /**
 84      *
 85      * @param strUrl 请求地址
 86      * @param params 请求参数
 87      * @param method 请求方法
 88      * @return  网络请求字符串
 89      * @throws Exception
 90      */
 91     public static String net(String strUrl, Map params,String method) throws Exception {
 92         HttpURLConnection conn = null;
 93         BufferedReader reader = null;
 94         String rs = null;
 95         try {
 96             StringBuffer sb = new StringBuffer();
 97             if(method==null || method.equals("GET")){
 98                 strUrl = strUrl+"?"+urlencode(params);
 99             }
100             URL url = new URL(strUrl);
101             conn = (HttpURLConnection) url.openConnection();
102             if(method==null || method.equals("GET")){
103                 conn.setRequestMethod("GET");
104             }else{
105                 conn.setRequestMethod("POST");
106                 conn.setDoOutput(true);
107             }
108             conn.setRequestProperty("User-agent", userAgent);
109             conn.setUseCaches(false);
110             conn.setConnectTimeout(DEF_CONN_TIMEOUT);
111             conn.setReadTimeout(DEF_READ_TIMEOUT);
112             conn.setInstanceFollowRedirects(false);
113             conn.connect();
114             if (params!= null && method.equals("POST")) {
115                 try {
116                     DataOutputStream out = new DataOutputStream(conn.getOutputStream());
117                         out.writeBytes(urlencode(params));
118                 } catch (Exception e) {
119                     // TODO: handle exception
120                 }
121             }
122             InputStream is = conn.getInputStream();
123             reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
124             String strRead = null;
125             while ((strRead = reader.readLine()) != null) {
126                 sb.append(strRead);
127             }
128             rs = sb.toString();
129         } catch (IOException e) {
130             e.printStackTrace();
131         } finally {
132             if (reader != null) {
133                 reader.close();
134             }
135             if (conn != null) {
136                 conn.disconnect();
137             }
138         }
139         return rs;
140     }
141  
142     //将map型转为请求参数型
143     public static String urlencode(Map<String,Object>data) {
144         StringBuilder sb = new StringBuilder();
145         for (Map.Entry i : data.entrySet()) {
146             try {
147                 sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
148             } catch (UnsupportedEncodingException e) {
149                 e.printStackTrace();
150             }
151         }
152         return sb.toString();
153     }
154 }    

 

posted @ 2019-09-07 19:37  Hi~Hi  阅读(1008)  评论(0编辑  收藏  举报
学习不分先后,知识不分多少;事无巨细,当以虚心求教;三人行,必有我师焉!!!wished for you successed !!!