这里以秒嘀科技为例,可以免费测试10元,每条0.06元,
1、注册账号
2、联系客服,审核签名
package com.answer.utils;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;import net.sf.json.JSONObject;/*** 获取短信验证码类* * @author */
public class GetMessage {/*** 用户ID*/public static final String ACCOUNT_SID = "";// 这里填写你在平台里的ACOUNT_SID/*** 密钥*/public static final String AUTH_TOKEN = "";/*** 请求地址前半部分 请求地址是固定的不用改*/public static final String BASE_URL = "https://api.miaodiyun.com/20150822/industrySMS/sendSMS";/*** * * @param to* @return String*/public static String getResult(String to) {String randNum = RandUtil.getRandNum();String smsContent = "【天涯旅游】您的验证码为" + randNum + ",请于" + 2 + "分钟内正确输入,如非本人操作,请忽略此短信。";String args = QueryUtil.queryArguments(ACCOUNT_SID, AUTH_TOKEN, smsContent, to);OutputStreamWriter out = null;BufferedReader br = null;StringBuffer sb = new StringBuffer();try {URL url = new URL(BASE_URL);URLConnection connection = url.openConnection(); // 打开链接connection.setDoOutput(true);connection.setDoInput(true);connection.setConnectTimeout(5000); // 设置链接超时connection.setReadTimeout(10000); // 设置读取超时// 提交数据out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");out.write(args);out.flush();// 读取返回数据br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));String line = "";while ((line = br.readLine()) != null) {sb.append(line);}} catch (Exception e) {e.printStackTrace();} finally {try {if (br != null) {br.close();}if (out != null) {out.close();}} catch (Exception e2) {e2.printStackTrace();}}JSONObject jsonObject = JSONObject.fromObject(sb.toString());System.out.println(jsonObject);Object object = jsonObject.get("respCode");System.out.println("状态码:" + object + "验证码:" + randNum);System.out.println(!object.equals("00000"));if (!object.equals("00000")) {return object.toString();} else {return randNum;}}//测试功能
// public static void main(String[] args) {
// String result = getResult("手机号码");
// System.out.println("验证码:"+randNum+"\t"+result);
// }
}工具类package com.answer.utils;import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;/*** 类描述:发送验证码工具类*/
public class QueryUtil {public static String queryArguments(String ACCOUNT_SID,String AUTH_TOKEN, String smsContent,String to) {String timestamp = getTimestamp(); //时间戳String sig = MD5(ACCOUNT_SID,AUTH_TOKEN,timestamp);//签名认证String str = "accountSid="+ACCOUNT_SID+"&smsContent="+smsContent+"&to="+to+"×tamp="+timestamp+"&sig="+sig+"&respDataType=JSON";return str;}/** 获取时间戳*/public static String getTimestamp(){SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");Date date = new Date();return sdf.format(date);}/*** MD5加密* @param args* @return*/public static String MD5(String... args){ //动态参数StringBuffer result = new StringBuffer();if (args == null || args.length == 0) {return "";} else {StringBuffer str = new StringBuffer();for (String string : args) {str.append(string);}System.out.println("加密前:\t"+str.toString());try {MessageDigest digest = MessageDigest.getInstance("MD5");byte[] bytes = digest.digest(str.toString().getBytes());for (byte b : bytes) {String hex = Integer.toHexString(b&0xff); //转化十六进制if (hex.length() == 1) {result.append("0"+hex);}else{result.append(hex);}}} catch (NoSuchAlgorithmException e) {e.printStackTrace();}}System.out.println("加密后:\t"+result.toString());return result.toString();}
}