package test.openApiTest;
import net.sf.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class ApiAutSmsTest {
public static void main(String[] args) {
String getTokenData = getToken();
JSONObject tokenJson = JSONObject.fromObject(getTokenData);
int status = tokenJson.getInt("status");
String info = tokenJson.getString("info");
String access_token = "";
if(status == 1){
JSONObject data = tokenJson.getJSONObject("data");
access_token = data.getString("access_token");
int expires_in = data.getInt("expires_in");//access_token的生命周期,单位是秒数。过期之后要重新获取
System.out.println("access_token:"+access_token+",expires_in:"+expires_in);
}
String app_key = "07a777471265f9c9a866c96f917dd7bf";
String sendSmsResult = sendSms(app_key,access_token);
System.out.println("sendSmsResult::" + sendSmsResult );//sendSmsResult::{"code":1000,"sendid":"2017090516580138555928044","msg":"短信提交成功"}
}
private static String getToken() {
try {
URL url = new URL("http://api.local.chuangcache.com/OAuth/authorize");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/json");
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
JSONObject obj = new JSONObject();
obj.put("appid", "appkey");//填写appkey (客服人员会给出)
obj.put("appsecret", "appsecret");//填写appsecret(客服人员会给出)
obj.put("grant_type", "client_credentials");
out.writeBytes(obj.toString());
out.flush();
out.close();
InputStream inStream = conn.getInputStream();
return new String(readInputStream(inStream), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String sendSms(String app_key,String access_token) {
OutputStreamWriter out = null;
try {
URL url = new URL("http://localhost:8982/api/sms/ordinary");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/json");
JSONObject obj = new JSONObject();
obj.put("access_token", access_token);
obj.put("app_key", app_key);
obj.put("mobile", "15300242462");//手机号码
obj.put("content", "您好,您的验证码为1212");//发送内容
obj.put("time", System.currentTimeMillis() + "");
out = new OutputStreamWriter(
conn.getOutputStream());
out.write(obj.toString());
out.flush();
InputStream inStream = conn.getInputStream();
return new String(readInputStream(inStream), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
}
}