手机短信验证
手机短信验证平台:秒滴科技:http://www.miaodiyun.com/
完整工具类的下载地址:https://files.cnblogs.com/files/han-guang-xue/phone.rar
package com.zhiyou.bigdata3.tools.phone; public class Config { /** * url前锟诫部锟斤拷 */ public static final String BASE_URL = "https://api.miaodiyun.com/20150822"; /** * 锟斤拷锟斤拷锟斤拷注锟斤拷锟较低筹拷远锟斤拷锟斤拷傻锟斤拷撕牛锟斤拷锟斤拷诠锟斤拷锟斤拷锟铰硷拷锟介看 */ public static final String ACCOUNT_SID = "8f6feca003cc426189f1412632006c24"; /** * 锟斤拷锟斤拷锟斤拷注锟斤拷锟较低筹拷远锟斤拷锟斤拷傻锟絋OKEN锟斤拷锟斤拷锟节癸拷锟斤拷锟斤拷录锟斤拷榭� */ public static final String AUTH_TOKEN = "bbda28f04b28424f95c5706f098139a2"; /** * 锟斤拷应锟斤拷锟斤拷锟斤拷锟斤拷, JSON锟斤拷XML */ public static final String RESP_DATA_TYPE = "json"; }
package com.zhiyou.bigdata3.tools.phone; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.codec.digest.DigestUtils; public class HttpUtil { /** * 锟斤拷锟斤拷通锟矫诧拷锟斤拷timestamp锟斤拷sig锟斤拷respDataType * * @return */ public static String createCommonParam() { // 时锟斤拷锟� SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String timestamp = sdf.format(new Date()); // 签锟斤拷 String sig = DigestUtils.md5Hex(Config.ACCOUNT_SID + Config.AUTH_TOKEN + timestamp); return "×tamp=" + timestamp + "&sig=" + sig + "&respDataType=" + Config.RESP_DATA_TYPE; } /** * post锟斤拷锟斤拷 * * @param url * 锟斤拷锟杰和诧拷锟斤拷 * @param body * 要post锟斤拷锟斤拷锟斤拷 * @return * @throws IOException */ public static String post(String url, String body) { System.out.println("url:" + System.lineSeparator() + url); System.out.println("body:" + System.lineSeparator() + body); String result = ""; try { OutputStreamWriter out = null; BufferedReader in = null; URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); // 锟斤拷锟斤拷锟斤拷锟接诧拷锟斤拷 conn.setDoOutput(true); conn.setDoInput(true); conn.setConnectTimeout(5000); conn.setReadTimeout(20000); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 锟结交锟斤拷锟斤拷 out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); out.write(body); out.flush(); // 锟斤拷取锟斤拷锟斤拷锟斤拷锟斤拷 in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line = ""; boolean firstLine = true; // 锟斤拷锟斤拷一锟叫诧拷锟接伙拷锟叫凤拷 while ((line = in.readLine()) != null) { if (firstLine) { firstLine = false; } else { result += System.lineSeparator(); } result += line; } } catch (Exception e) { e.printStackTrace(); } return result; } /** * 锟截碉拷锟斤拷锟皆癸拷锟竭凤拷锟斤拷 * * @param url * @param reqStr * @return */ public static String postHuiDiao(String url, String body) { String result = ""; try { OutputStreamWriter out = null; BufferedReader in = null; URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); // 锟斤拷锟斤拷锟斤拷锟接诧拷锟斤拷 conn.setDoOutput(true); conn.setDoInput(true); conn.setConnectTimeout(5000); conn.setReadTimeout(20000); // 锟结交锟斤拷锟斤拷 out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); out.write(body); out.flush(); // 锟斤拷取锟斤拷锟斤拷锟斤拷锟斤拷 in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line = ""; boolean firstLine = true; // 锟斤拷锟斤拷一锟叫诧拷锟接伙拷锟叫凤拷 while ((line = in.readLine()) != null) { if (firstLine) { firstLine = false; } else { result += System.lineSeparator(); } result += line; } } catch (Exception e) { e.printStackTrace(); } return result; } }
package com.zhiyou.bigdata3.tools.phone; import java.net.URLEncoder; public class IndustrySMS { private static String operation = "/industrySMS/sendSMS"; private static String accountSid = Config.ACCOUNT_SID; private static String code = smsCode(); //登录验证码:{1},如非本人操作,请忽略此短信。 //您的验证码为{1},请于{2}分钟内正确输入,如非本人操作,请忽略此短信。 private static String smsContent = "【智游集团】您的验证码为"+code+",请于2分钟内正确输入,如非本人操作,请忽略此短信。"; /** * 验证码通知短信 */ public static String execute(String to) { String tmpSmsContent = null; try{ tmpSmsContent = URLEncoder.encode(smsContent, "UTF-8"); }catch(Exception e){ } String url = Config.BASE_URL + operation; String body = "accountSid=" + accountSid + "&to=" + to + "&smsContent=" + tmpSmsContent + HttpUtil.createCommonParam(); // 提交请求 String result = HttpUtil.post(url, body); System.out.println("result:" + System.lineSeparator() + result); return code; } //创建验证码 public static String smsCode(){ String random=(int)((Math.random()*9+1)*100000)+""; System.out.println("验证码:"+random); return random; } }
package com.zhiyou.bigdata3.tools.phone; public class Test { /** * @param args */ public static void main(String[] args) { String str = IndustrySMS.execute("13673711016"); System.out.println(str); } public static String phoneCode(String phone) { String str = IndustrySMS.execute(phone); System.out.println(str); return str; } }
需要的jar包
package com.zhiyou.bigdata3.tools.phone; public class Test { /** * @param args */ public static void main(String[] args) { String str = IndustrySMS.execute("13673711016"); System.out.println(str); } public static String phoneCode(String phone) { String str = IndustrySMS.execute(phone); System.out.println(str); return str; } }
本文来自博客园踩坑狭,作者:韩若明瞳,转载请注明原文链接:https://www.cnblogs.com/han-guang-xue/p/9852510.html