我使用的是秒嘀科技的接口,新用户有一些免费的短信发送数量。
配置类
其中XXX部分是注册账号后会给你的,要记住。
package yan_zhen_ma;
/**
* 配置类,验证码接口详细访问http://www.miaodiyun.com/doc/https_sms.html
* @author chenlun
*
*/
public class Config {
public static final String url="https://api.miaodiyun.com/20150822";
public static final String account_sid="xxx";
public static final String auth_token="xxx";
public static final String data_type="json";
}
工具类
package yan_zhen_ma;
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参数必选
* @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.data_type;
}
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 con=realUrl.openConnection();
//允许连接输入输出流
con.setDoOutput(true);
con.setDoInput(true);
con.setConnectTimeout(5000);
con.setReadTimeout(20000);
//请求的内容需要编码
con.setRequestProperty("content-Type", "application/x-www-form-urlencoded");
out=new OutputStreamWriter(con.getOutputStream(),"utf-8");
out.write(body);
out.flush();
in=new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8"));
String line="";
boolean firstLine=true;//true:读第一行不加换行
while((line=in.readLine())!=null) {
if(firstLine)
firstLine=false;
else result+=System.lineSeparator();
result+=line;
}
}catch(Exception e) {
e.printStackTrace();
}
return result;
}
public static String postCallBack(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 yan_zhen_ma;
import java.net.URLEncoder;
public class Sms {
private static String op = "/industrySMS/sendSMS";
private static String accountSid = Config.account_sid;
private static String to = "对方电话号码";
private static String code = smsCode();
//验证码为下面方法生成的,这里简单提供了,字符串格式与网站上要一致
private static String smsContent = "【一揽芳华科技】您的验证码为" + 201314 + ",请于" + 2 + "分钟内回复心意给你爱的人,祝你心想事成!";
private static String smsCode() {
String random = (int) ((Math.random() * 9 + 1) * 100000) + "";
System.out.println("验证码:" + random);
return random;
}
public static void execute() {
String tmpSmsContent = null;
try {
tmpSmsContent = URLEncoder.encode(smsContent, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
String url = Config.url + op;//提交到的url
//3个参数都是必选
String body = "accountSid=" + accountSid + "&to=" + to + "&smsContent=" + tmpSmsContent
+ HttpUtil.createCommonParam();
// 提交请求
String result = HttpUtil.post(url, body);
System.out.println("result:" + System.lineSeparator() + result);
}
}
调用execute方法即可将验证码发送给目的手机号。短信验证码6分钱一条,新用户用完送的后就要购买了,最低充值500元。也当是玩玩了,按着接口文档操作还是很简单的。