168 lines
5.5 KiB
Java
Raw Normal View History

2023-08-04 17:25:38 +08:00
package com.zhgd.xmgl.util;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 短信发送工具类
*/
public class SMS {
private static String session_value;
private static String host = "47.106.214.54:8080";
public static void main(String[] args) {
//updateRoadCheckData("20221026");
//AutoSync("2022-10-29");
//发送短信
JSONObject params = new JSONObject();
params.put("phone", "17373303529");
params.put("project", "sms测试项目1");
params.put("alarmInfo", "预警类型1");
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);
ret = SMS.post("http://"+host+"/SmsServer/api/sendSms", params);
System.out.println(ret);
}
public static void sendToPhone_YZM(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);
ret = SMS.post("http://"+host+"/SmsServer/api/sendSms_YZM", params);
System.out.println(ret);
}
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;
}
}