jiangping
2023-12-25 6707b8df16cf531d6654d8a3b429de45f5fe9db3
server/services/src/main/java/com/doumee/core/utils/HttpsUtil.java
@@ -30,7 +30,6 @@
    public static String post(String url, String data, String contentType, boolean ignoreSSL) {
        return connection(url, "POST", data, contentType, ignoreSSL);
    }
    public static String connection(String url,String method,String data,String contentType,boolean ignoreSSL){
        HttpsURLConnection connection = null;
        try {
@@ -89,7 +88,48 @@
        }
        return null;
    }
    public static InputStream postJson(String url, String data){
        HttpsURLConnection connection = null;
        try {
            URL _url = new URL(url);
            connection = (HttpsURLConnection) _url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.connect();
            //信任所有ssl证书和主机
            TrustManager[] trustManagers = {new HttpsTrustManager()};
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, trustManagers, new SecureRandom());
            connection.setSSLSocketFactory(context.getSocketFactory());
            connection.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
            if(data != null){
                OutputStream outputStream = connection.getOutputStream();
                outputStream.write(data.getBytes("utf-8"));
                outputStream.close();
            }
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                InputStream is = connection.getInputStream();
                return is;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(connection != null){
           //     connection.disconnect();
            }
        }
        return null;
    }
    /**
     * 发送get请求
     * @param url 请求URL