https方式请求 且过滤证书

package com.zllms.web.core.aic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.X509Certificate;

public class AiChannel {
public static void main(String[] args) {
// String url = " https://129.226.223.13/zllms/model/text_tag";
String url = “http://22.50.21.5:8000/text_tag”;
JSONObject json = new JSONObject();
json.put(“sentence”, “画一只猫”);
String data = json.toJSONString();

    try {
        String response = doPostRequests(url, data);
        JSONObject jsonObject = JSON.parseObject(response);
        String sentenceTag = jsonObject.getString("channel_type");
        System.out.println("sentence_tag: " + sentenceTag);
        System.out.println(response);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static String doPostRequest(String url, String data) throws Exception {
    URL endpoint = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setDoOutput(true);

    OutputStream outputStream = connection.getOutputStream();
    outputStream.write(data.getBytes());
    outputStream.flush();

    if (connection.getResponseCode() == 200) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        return response.toString();
    } else {
        throw new Exception("HTTP POST request failed with error code: " + connection.getResponseCode());
    }
}

/**
 * https方式请求 证书过滤
 * @param url
 * @param data
 * @return
 * @throws Exception
 */
public static String doPostRequests(String url, String data) throws Exception {
    // 创建信任所有证书的 TrustManager
    TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }
                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
    };

    // 安装信任管理器
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    // 创建所有主机名验证器
    HostnameVerifier allHostsValid = (hostname, session) -> true;
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

    // 打开连接
    URL endpoint = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection();
    if (connection instanceof HttpsURLConnection) {
        ((HttpsURLConnection) connection).setSSLSocketFactory(sc.getSocketFactory());
        ((HttpsURLConnection) connection).setHostnameVerifier(allHostsValid);
    }
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
    connection.setDoOutput(true);

    // 发送请求数据
    try (OutputStream outputStream = connection.getOutputStream()) {
        outputStream.write(data.getBytes("UTF-8"));
        outputStream.flush();
    }

    // 根据响应码处理响应
    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }

            return response.toString();
        }
    } else {
        throw new Exception("HTTP POST request failed with error code: " + connection.getResponseCode());
    }
}

}

你可能感兴趣的:(https,java)