xin
2025-06-04 b3131693e75aabcd24718a78232b3b0671cdf944
短信测试
2 files added
3 files modified
240 ■■■■■ changed files
oying-common/src/main/java/com/oying/utils/HttpRequest.java 13 ●●●●● patch | view | raw | blame | history
oying-system/src/main/resources/config/application-dev.yml 5 ●●●●● patch | view | raw | blame | history
oying-system/src/main/resources/config/application-prod.yml 5 ●●●●● patch | view | raw | blame | history
oying-tools/src/main/java/com/oying/utils/SendMessageUtils.java 35 ●●●●● patch | view | raw | blame | history
oying-tools/src/main/java/com/oying/utils/Test.java 182 ●●●●● patch | view | raw | blame | history
oying-common/src/main/java/com/oying/utils/HttpRequest.java
@@ -79,11 +79,20 @@
        return headers;
    }
    private static HttpHeaders get() {
        HttpHeaders headers = new HttpHeaders();
        headers.set("accept", "*/*");
        headers.set("connection", "Keep-Alive");
        headers.set("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        return headers;
    }
    /**
     * http返回String
     */
    public static String exchangeString(HttpMethod httpMethod, String url, Map<String, Object> map) {
        HttpHeaders headers = getHeaders();
        HttpHeaders headers = get();
        HttpEntity<Object> httpEntity = new HttpEntity<>(map, headers);
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.exchange(url, httpMethod, httpEntity, String.class).getBody();
@@ -98,4 +107,6 @@
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.exchange(url, httpMethod, httpEntity, JSONObject.class).getBody();
    }
}
oying-system/src/main/resources/config/application-dev.yml
@@ -176,3 +176,8 @@
  access_key_secret: QpE58YEFtgoIwUoGNlN5JlNY7t6qVu7vMkix8gAI
  bucket: oying
  endpoint: https://obs.cn-southwest-2.myhuaweicloud.com
winner-look:
  url: https://118.178.116.15:8443/
  user-code: CQLYSXYJ
  user-pass: lych1205!
oying-system/src/main/resources/config/application-prod.yml
@@ -186,3 +186,8 @@
  access_key_secret: QpE58YEFtgoIwUoGNlN5JlNY7t6qVu7vMkix8gAI
  bucket: oying
  endpoint: https://obs.cn-southwest-2.myhuaweicloud.com
winner-look:
  url: https://118.178.116.15:8443/
  user-code: CQLYSXYJ
  user-pass: lych1205!
oying-tools/src/main/java/com/oying/utils/SendMessageUtils.java
New file
@@ -0,0 +1,35 @@
package com.oying.utils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import java.util.HashMap;
import java.util.Map;
/**
 * @author xin
 * @description
 * @date 2025/6/4 01:10
 */
@Data
public class SendMessageUtils {
    @Value("${winner-look.url}")
    private String url;
    @Value("${winner-look.user-code}")
    private String userCode;
    @Value("${winner-look.user-pass}")
    private String userPass;
    public static void main(String[] args)  {
        String url = "https://118.178.116.15:8443/winnerrxd/api/trigger/SendMsg";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userCode", "CQLYSXYJ");
        paramMap.put("userPass", "lych1205!");
        paramMap.put("DesNo", "15213186640");
        paramMap.put("Msg", "短信内容【立研】");
        paramMap.put("smsType", "101");
        System.out.println(HttpRequest.exchangeString(HttpMethod.POST, url, paramMap));
    }
}
oying-tools/src/main/java/com/oying/utils/Test.java
New file
@@ -0,0 +1,182 @@
package com.oying.utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
/**
 * @author xin
 * @description
 * @date 2025/6/4 01:48
 */
public class Test {
    public static void main(String[] args) {
        String url = "https://118.178.116.15:8443/winnerrxd/api/trigger/SendMsg ";
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("userCode", "CQLYSXYJ");
        paramMap.put("userPass", "lych1205!");
        paramMap.put("DesNo", "15213186640");
        paramMap.put("Msg", "短信内容【立研】");
        paramMap.put("smsType","短信类型");
        try {
//GET请求
            httpsGet(url, paramMap);
//POST请求
            httpsPost(url, paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("调用https失败:"+e);
        }
    }
    private static class TrustAnyTrustManager implements X509TrustManager {
        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }
        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }
    }
    private static class TrustAnyHostnameVerifier implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }
    public static String httpsGet(String url,Map<String, String> params) throws
            Exception
    {
        String result = "";
        BufferedReader in = null;
        try {
            String urlStr = url + "?" + getParamStr(params);
            System.out.println("GET请求的URL为:"+urlStr);
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
                    new java.security.SecureRandom());
            URL realUrl = new URL(urlStr);
            // 打开和URL之间的连接
            HttpsURLConnection connection = (HttpsURLConnection)
                    realUrl.openConnection();
            //设置https相关属性
            connection.setSSLSocketFactory(sc.getSocketFactory());
            connection.setHostnameVerifier(new TrustAnyHostnameVerifier());
            connection.setDoOutput(true);
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new
                    InputStreamReader(connection.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            System.out.println("获取的结果为:"+result);
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            //e.printStackTrace();
            throw e;
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                //e2.printStackTrace();
                throw e2;
            }
        }
        return result;
    }
    public static String httpsPost(String url, Map<String, String> params)
            throws Exception{
        String result = "";
        BufferedReader in = null;
        String content = getParamStr(params);
        try {
            System.out.println("POST请求的URL为:"+url);
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
                    new java.security.SecureRandom());
            URL console = new URL(url);
            HttpsURLConnection connection = (HttpsURLConnection)
                    console.openConnection();
            connection.setSSLSocketFactory(sc.getSocketFactory());
            connection.setHostnameVerifier(new TrustAnyHostnameVerifier());
            connection.setDoOutput(true);
            connection.connect();
            DataOutputStream out = new
                    DataOutputStream(connection.getOutputStream());
            out.write(content.getBytes("UTF-8"));
            // 刷新、关闭
            out.flush();
            out.close();
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new
                    InputStreamReader(connection.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            System.out.println("获取的结果为:"+result);
        } catch (Exception e) {
            System.out.println("发送POST请求出现异常!" + e);
            //e.printStackTrace();
            throw e;
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                //e2.printStackTrace();
                throw e2;
            }
        }
        return result;
    }
    private static String getParamStr(Map<String, String> params)
    {
        String paramStr="";
        if(params != null && params.size() >0){
            // 获取参数列表组成参数字符串
            for (String key : params.keySet()) {
                paramStr+=key+"="+params.get(key)+"&";
            }
            //去除最后一个"&"
            paramStr=paramStr.substring(0, paramStr.length()-1);
        }
        return paramStr;
    }
}