雲信短信模板

雲信官網:

http://www.sms.cn/sms_api.html#item2

栗子:

/**
 * @ClassName: Sms
 * @version: 1.0
 * @description: 雲信
 * @author: zhaonian
 **/
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.hwqh.huawenstockuser.common.ConstantUtil;
import com.hwqh.huawenstockuser.utils.http.HttpRequestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * 最新短信接口
 */
public class Sms {
    private static Logger logger = LoggerFactory.getLogger(Sms.class);
    //在註冊的用戶名
    public static final String uid = "註冊名";
    //在註冊的密碼
    public static final String pwd = "註冊密碼";
    //在註冊的密碼
    public static final String ac = "send";
    //url
    public static final String httpUrl = "http://api.sms.cn/sms/?";


    public static void main(String[] args) throws Exception {

        String s = sendSms("手機號", "模板號碼", "驗證碼");
        System.out.println(s);


    }
    /**
     * 登錄發送短信驗證碼
     * @param mobile
     * @param template
     * @param code
     * @return
     * @throws Exception
     */
    public static String sendSms(String mobile, String template, String code) throws Exception {
        StringBuffer httpArg = new StringBuffer();
        httpArg.append(httpUrl);
        httpArg.append("ac=").append(ac).append("&");
        httpArg.append("uid=").append(uid).append("&");
        httpArg.append("pwd=").append(pwd).append("&");
        httpArg.append("template=").append(template).append("&");
        httpArg.append("mobile=").append(mobile).append("&");
        httpArg.append("content=");
        httpArg.append("{\"code\":\"" + code + "\"}");
        String s1 = HttpRequestUtil.sendGet(httpArg.toString());
        JSONObject jo = JSONObject.parseObject(s1);
        String stat = jo.getString("stat");
        return stat;
    }

    /**
     * 向指定URL發送GET方法的請求
     *
     * @param url
     *            發送請求的URL
     * @param param
     *            請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
     * @return URL 所代表遠程資源的響應結果
     */
    public static String sendGet(String url) {
        String result = "";
        StringBuilder jsonStr = new StringBuilder();
        BufferedReader in = null;
        try {
            String urlNameString = url;
            URL realUrl = new URL(urlNameString);
            // 打開和URL之間的連接
            URLConnection connection = realUrl.openConnection();
            // 設置通用的請求屬性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立實際的連接
            connection.connect();
            // 獲取所有響應頭字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍歷所有的響應頭字段
            for (String key : map.keySet()) {
                logger.info(key + "--->" + map.get(key));
            }

            //ConstantUtil.UTF_CODE 編碼格式
            InputStreamReader reader = new InputStreamReader(connection.getInputStream(), ConstantUtil.UTF_CODE);
            char[] buff = new char[1024];
            int length = 0;
            while ((length = reader.read(buff)) != -1) {
                result = new String(buff, 0, length);
                jsonStr.append(result);
            }


            Gson gson = new Gson();
            Map temp  = gson.fromJson("", Map.class);

        } catch (Exception e) {
            logger.info("發送GET請求出現異常!" + e);
            e.printStackTrace();
        }
        // 使用finally塊來關閉輸入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return jsonStr.toString();
    }


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章