package cn.geekss.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.json.JsonObject;
import org.json.JSONObject;
public class GetMessageCode1 {
// 請求的地址
protected static final String QUERY_PATH = "https://api.miaodiyun.com/20150822/industrySMS/sendSMS";
// 開發者主帳號id ACCOUNT_SID 由32位英文字母和阿拉伯數字組成的帳號惟一標識符0
protected static final String ACCOUNT_SID = "3832ef3b8a434462ba3b987db0bcddd5";
// token 簽名用
protected static final String AUTH_TOKEN = "da47afd7203c40188830eaec650cfd3d";
/**
* 發送驗證
*
* @param phone
* @return
*/
public static String sendCode(String phone) {
// 驗證碼
String code = getCode();
// 時間戳
String timeStamp = getTimeStamp();
// 獲取簽名
String sig = getSig(ACCOUNT_SID, AUTH_TOKEN, timeStamp);
// 內容
String content = "【博客園】登陸驗證碼:" + code + ",如非本人操做,請忽略此短信。";
//輸出流
OutputStreamWriter writer = null;
//輸入流
BufferedReader bufferedReader = null;
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(QUERY_PATH);
HttpURLConnection httpUrlConnection = (HttpURLConnection) url
.openConnection();
httpUrlConnection.setRequestMethod("POST");
// 是否容許寫入
httpUrlConnection.setDoInput(true);
// 是否容許輸出
httpUrlConnection.setDoOutput(true);
// 5秒
httpUrlConnection.setConnectTimeout(5000);
httpUrlConnection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
writer = new OutputStreamWriter(
httpUrlConnection.getOutputStream(), "UTF-8");
// 請求的參數
String url_para = getQueryPara(ACCOUNT_SID, content, phone,
timeStamp, sig, "JSON");
writer.write(url_para);
System.out.println(url_para);
writer.flush();
/*
* reader = new
* InputStreamReader(httpUrlConnection.getInputStream(),"UTF-8");
*/
bufferedReader = new BufferedReader(new InputStreamReader(
httpUrlConnection.getInputStream(), "UTF-8"));
String stringline = "";
while ((stringline = bufferedReader.readLine()) != null) {
sb.append(stringline);
}
} catch (Exception e) {
e.printStackTrace();
}
JSONObject object = new JSONObject(sb.toString());
System.out.println(object);
String defaultResponse = "00000";
String str = (String) object.get("respCode");
if (str.equals(defaultResponse)) {
return code;
} else {
return defaultResponse;
}
}
/**
* 請求的參數集合
*
* @param accountSid
* @param smsContent
* @param to
* @param timestamp
* @param sig
* @param resultDataType
* @return
*/
public static String getQueryPara(String accountSid, String smsContent,
String to, String timestamp, String sig, String resultDataType) {
String url = "accountSid=" + accountSid + "&smsContent=" + smsContent
+ "&to=" + to + "×tamp=" + timestamp + "&sig=" + sig
+ "&respDataType=" + resultDataType;
return url;
}
/**
* 獲取時間戳
*
* @return
*/
public static String getTimeStamp() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(new Date());
}
/**
* 簽名 MD5 加密
*
* @return
*/
public static String getSig(String account_Sid, String auth_Token,
String timeStamp) {
String sig = account_Sid + auth_Token + timeStamp;
StringBuilder sb = new StringBuilder();
// 獲取 MD5 加密
try {
// 獲取MD5加密類
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] bytes = digest.digest(sig.getBytes());
for (byte b : bytes) {
String hex = Integer.toHexString(b & 0xff);
if (hex.length() == 1) {
sb.append("0" + hex);
} else {
sb.append(hex);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* 獲取發送的驗證碼
*
* @return
*/
public static String getCode() {
String code = new Random().nextInt(1000000) + "";
if (code.length() < 6) {
return getCode();
}
return code;
}
public static void main(String[] args) {
System.out.println(sendCode("13189031999"));
}
}
java