短信验证码接口

我使用的是秒嘀科技的接口,新用户有一些免费的短信发送数量。

配置类

其中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 "&timestamp=" + 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元。也当是玩玩了,按着接口文档操作还是很简单的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值