手機短信驗證

手機短信驗證平臺:秒滴科技: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 "&timestamp=" + 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包java

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;

    }

}
相關文章
相關標籤/搜索