java通过短信发送验证码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/* * 如uid是:test,登录密码是:123123 pwd=md5(123123test),即 pwd=b9887c5ebb23ebb294acab183ecf0769 线生成地址:http://www.sms.cn/password */ private static final String addr = "http://api.sms.cn/mt/"; private static final String userId = "xxxxxx"; private static final String pwd = "b9887c5ebb23ebb294acab183ecf0769"; private static final String encode = "utf8"; public static String send(String code, String mobile) throws Exception { String msgContent = "XXXXXXXXXXXXXXXXXXXXXX欢迎您的加入,验证码为:" + code; //组建请求 String straddr = addr + "?uid=" + userId + "&pwd=" + pwd + "&mobile=" + mobile + "&encode=" + encode + "&content=" + msgContent; StringBuffer sb = new StringBuffer(straddr); System.out.println("URL:" + sb); //发送请求 URL url = new URL(sb.toString()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); BufferedReader in = new BufferedReader(new InputStreamReader( url.openStream(), "gbk")); //返回结果 String inputline = in.readLine(); return inputline; } |