package com.stylefeng.guns.modular.system.util;
|
|
import com.alibaba.fastjson.JSONObject;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.stereotype.Component;
|
|
import java.io.BufferedReader;
|
import java.io.InputStreamReader;
|
import java.io.OutputStream;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
|
@Component
|
public class SmsUtil {
|
|
private static Logger logger = LoggerFactory.getLogger(SmsUtil.class);
|
|
public static void main(String[] args) {
|
//短信下发
|
String sendUrl = "https://xxx/msg/send/json";
|
Map map = new HashMap();
|
map.put("account","N*******");//API账号
|
map.put("password","************");//API密码
|
map.put("msg","您本次验证码为:{0}。请勿泄露他人,如非本人操作可勿略本短信");//短信内容
|
map.put("phone","15300000000");//手机号
|
map.put("report","true");//是否需要状态报告
|
map.put("extend","123");//自定义扩展码
|
JSONObject js = (JSONObject) JSONObject.toJSON(map);
|
logger.debug(sendSmsByPost(sendUrl,js.toString()));
|
//查询余额
|
String balanceUrl = "https://xxx/msg/balance/json";
|
Map map1 = new HashMap();
|
map1.put("account","N*******");
|
map1.put("password","************");
|
JSONObject js1 = (JSONObject) JSONObject.toJSON(map1);
|
logger.debug(sendSmsByPost(balanceUrl,js1.toString()));
|
}
|
|
|
public static String sendSmsByPost(String path, String postContent) {
|
URL url = null;
|
try {
|
url = new URL(path);
|
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
|
httpURLConnection.setRequestMethod("POST");
|
httpURLConnection.setConnectTimeout(10000);
|
httpURLConnection.setReadTimeout(10000);
|
httpURLConnection.setDoOutput(true);
|
httpURLConnection.setDoInput(true);
|
httpURLConnection.setRequestProperty("Charset", "UTF-8");
|
httpURLConnection.setRequestProperty("Content-Type", "application/json");
|
httpURLConnection.connect();
|
OutputStream os=httpURLConnection.getOutputStream();
|
os.write(postContent.getBytes("UTF-8"));
|
os.flush();
|
StringBuilder sb = new StringBuilder();
|
int httpRspCode = httpURLConnection.getResponseCode();
|
if (httpRspCode == HttpURLConnection.HTTP_OK) {
|
BufferedReader br = new BufferedReader(
|
new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
|
String line = null;
|
while ((line = br.readLine()) != null) {
|
sb.append(line);
|
}
|
br.close();
|
return sb.toString();
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
}
|