上个图
工具类
package org.springblade.common.utils;
import com.alibaba.fastjson.JSONObject;
import org.springblade.modules.tc.mas.Submit;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
/**
* 云MAS平台 短信工具类
*/
public class MasUtil {
public static void main(String[] args) throws Exception {
String urlString = "http:/xxxx/sms/norsubmit";
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/xml");
String postData = GetSmsParam();
OutputStream outputStream = connection.getOutputStream();
outputStream.write(postData.getBytes("UTF-8"));
outputStream.flush();
outputStream.close();
int responseCode = connection.getResponseCode();
System.out.println(responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuffer response = new StringBuffer();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
System.out.println("Response: " + response.toString());
} else {
System.out.println("POST request failed with response code: " + responseCode);
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* base64
*
* @param input
* @return
*/
private static String encodeBase64String(String input) {
// 将字符串转换为字节数组
byte[] bytes = input.getBytes();
// 对字节数组进行Base64编码
String base64Encoded = Base64.getEncoder().encodeToString(bytes);
// 输出Base64编码后的字符串
System.out.println("Base64编码后的字符串:" + base64Encoded);
return base64Encoded;
}
/**
* MD5加密(32位小写)
*
* @param input
* @return
*/
private static String encryptToMD5(String input) {
try {
// 创建MessageDigest对象并指定算法为MD5
MessageDigest md = MessageDigest.getInstance("MD5");
// 将字符串转换为字节数组并进行加密
byte[] mdBytes = md.digest(input.getBytes());
// 将字节数组转换为对应的十六进制字符串
StringBuilder hexString = new StringBuilder();
for (byte mdByte : mdBytes) {
// 通过位运算和与操作将字节转换为十六进制字符串
String hex = Integer.toHexString(0xff & mdByte);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
// 输出加密后的字符串
System.out.println("加密后的字符串:" + hexString.toString());
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
// 处理算法不存在的异常
e.printStackTrace();
}
return input;
}
public static String GetSmsParam() throws Exception {
Submit submit = new Submit();
submit.setEcName("接口联调账号");
submit.setApId("rtzhxx");
submit.setSecretKey("asasas");
submit.setMobiles("asas7");
submit.setContent("移as生活");
submit.setSign("bM1asa);
submit.setAddSerial("");
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(submit.getEcName());
stringBuffer.append(submit.getApId());
stringBuffer.append(submit.getSecretKey());
stringBuffer.append(submit.getMobiles());
stringBuffer.append(submit.getContent());
stringBuffer.append(submit.getSign());
stringBuffer.append(submit.getAddSerial());
String selfMac = encryptToMD5(stringBuffer.toString());
System.out.println("selfMac:" + selfMac);
submit.setMac(selfMac);
String param = JSONObject.toJSONString(submit);
System.out.println("param:" + param);
String encode = encodeBase64String(param);
return encode;
}
}
本文转载自: https://blog.csdn.net/chengchong_cc/article/details/137881806
版权归原作者 chengchong_cc 所有, 如有侵权,请联系我们删除。
版权归原作者 chengchong_cc 所有, 如有侵权,请联系我们删除。