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());
    }
}

}

相关推荐
魔道不误砍柴功43 分钟前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
NiNg_1_23443 分钟前
SpringBoot整合SpringSecurity实现密码加密解密、登录认证退出功能
java·spring boot·后端
闲晨1 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
测开小菜鸟2 小时前
使用python向钉钉群聊发送消息
java·python·钉钉
P.H. Infinity3 小时前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天3 小时前
java的threadlocal为何内存泄漏
java
caridle3 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
^velpro^4 小时前
数据库连接池的创建
java·开发语言·数据库
苹果醋34 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
秋の花4 小时前
【JAVA基础】Java集合基础
java·开发语言·windows