2023-10-11 11:16:24 +08:00

174 lines
5.9 KiB
Java

package com.zhgd.xmgl.util;
import com.alibaba.fastjson.JSONObject;
import com.zhgd.mybatis.Aes;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* 短信发送工具类
*/
public class SMS {
private static String session_value;
private static String host = "192.168.121.148:80";
public static void main(String[] args) {
//updateRoadCheckData("20221026");
//AutoSync("2022-10-29");
//发送短信
JSONObject params = new JSONObject();
List<String> phone = new ArrayList<>();
phone.add("17373303529");
params.put("phone", Aes.hzEncrypt(phone.toString()));
// params.put("code", Aes.hzEncrypt("123456"));
params.put("project", Aes.hzEncrypt("项目管理系统"));
params.put("alarm", Aes.hzEncrypt("施工进度逾期"));
SMS.sendToPhone(params);
//发送验证码
// JSONObject params = new JSONObject();
// params.put("phone", "17373303529");
// params.put("code", "123321");
// SMS.sendToPhone_YZM(params);
}
/**
* 发送短信信息
* @param params
*/
public static void sendToPhone(JSONObject params) {
// JSONObject jo = new JSONObject();
// jo.put("user", "smsUser");
// jo.put("passwd", "sjhA86JDFH$2356_sdfAj123");
// String ret = SMS.login("http://"+host+"/SmsServer/UI/login", jo.toJSONString());
// System.out.println(ret);
try {
String result = HttpUtil.doPost("http://"+host+"/sms/message/informAlarm", params.toJSONString());
System.out.println(result);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void sendToPhone_YZM(JSONObject params) {
try {
String result = HttpUtil.doPost("http://" + host + "/sms/message/code", params.toJSONString());
System.out.println(result);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String login(String urlPath, String sendStr) {
String result = "";
String line = "";
BufferedReader reader = null;
try {
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "text/plain;charset=UTF-8");
conn.setRequestProperty("Accept", "*/*");
if (sendStr != null && sendStr.trim().length() > 0) {
byte[] writebytes = sendStr.getBytes();
conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));
OutputStream outwritestream = conn.getOutputStream();
outwritestream.write(sendStr.getBytes());
outwritestream.flush();
outwritestream.close();
conn.getResponseCode();
}
if (conn.getResponseCode() == 200) {
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
while ((line = reader.readLine()) != null) {
result += line;
}
}
session_value=conn.getHeaderField("Set-Cookie");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
private static String post(String urlPath, JSONObject param) {
if(param==null) {
param=new JSONObject();
}
String Json = param.toJSONString();
String result = "";
String line = "";
BufferedReader reader = null;
try {
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "text/plain;charset=UTF-8");
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Cookie", session_value);
if (Json != null && Json.trim().length() > 0) {
byte[] writebytes = Json.getBytes();
conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));
OutputStream outwritestream = conn.getOutputStream();
outwritestream.write(Json.getBytes());
outwritestream.flush();
outwritestream.close();
conn.getResponseCode();
}
if (conn.getResponseCode() == 200) {
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
while ((line = reader.readLine()) != null) {
result += line;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
}