Java调用https类接口/地址的工具类。
其中Post方式未经过测试,请谨慎使用。
javapackage com.hlkj.dncc.util;
import com.hlkj.core.util.JsonUtils;
import javax.net.ssl.*;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
 * https请求工具类
 *
 * @author DingDangDog
 * @date 2022/12/29
 */
public class HttpsUtils {
    private HttpsUtils() {
    }
    private static SSLSocketFactory socketFactory;
    /**
     * 执行GET方式接口调用
     *
     * @param url 接口地址
     * @return String 返回数据
     * @throws IOException              e
     * @throws NoSuchAlgorithmException e
     * @throws KeyManagementException   e
     */
    public static String doGet(String url) throws IOException, NoSuchAlgorithmException, KeyManagementException {
        HttpsURLConnection connection = getConnection(url);
        connection.setRequestMethod("GET");
        connection.connect();
        // 读取返回数据
        StringBuilder builder = new StringBuilder();
        try (InputStream inputStream = connection.getInputStream();
             InputStreamReader isr = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
             BufferedReader br = new BufferedReader(isr)) {
            String line = null;
            while ((line = br.readLine()) != null) {
                builder.append(line);
            }
        }
        return builder.toString();
    }
    public static String doPost(String url, Object body) throws IOException, NoSuchAlgorithmException, KeyManagementException {
        HttpsURLConnection connection = getConnection(url);
        connection.setRequestMethod("POST");
        connection.connect();
        // 发送数据
        try (OutputStream outputStream = connection.getOutputStream()) {
            outputStream.write(JsonUtils.objectToString(body).getBytes(StandardCharsets.UTF_8));
        }
        // 读取数据
        StringBuilder builder = new StringBuilder();
        try (InputStream inputStream = connection.getInputStream();
             InputStreamReader isr = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
             BufferedReader br = new BufferedReader(isr)) {
            String line = null;
            while ((line = br.readLine()) != null) {
                builder.append(line);
            }
        }
        return builder.toString();
    }
    /**
     * ssl通信工厂
     *
     * @return SSLSocketFactory
     * @throws KeyManagementException   e
     * @throws NoSuchAlgorithmException e
     */
    private static SSLSocketFactory getSslSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
        if (null != HttpsUtils.socketFactory) {
            return HttpsUtils.socketFactory;
        }
        SSLContext ssl = SSLContext.getInstance("SSL");
        TrustManager[] trustManagers = {new X509TrustManager() {
            /**
             * 实例化一个信任连接管理器
             * 空实现是所有的连接都能访问
             */
            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            }
            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            }
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        }};
        ssl.init(null, trustManagers, new SecureRandom());
        HttpsUtils.socketFactory = ssl.getSocketFactory();
        return HttpsUtils.socketFactory;
    }
    /**
     * 连接统一构造方法
     *
     * @param url 接口地址
     * @return HttpsURLConnection 连接器
     * @throws IOException              e
     * @throws NoSuchAlgorithmException e
     * @throws KeyManagementException   e
     */
    private static HttpsURLConnection getConnection(String url) throws IOException, NoSuchAlgorithmException, KeyManagementException {
        URL urlObj = new URL(url);
        HttpsURLConnection urlConnection = (HttpsURLConnection) urlObj.openConnection();
        urlConnection.setSSLSocketFactory(getSslSocketFactory());
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
        urlConnection.setRequestProperty("Accept-Charset", "utf-8");
        return urlConnection;
    }
}
本文作者:DingDangDog
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!