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

}

相关推荐
知其然亦知其所以然4 分钟前
RAG 结果太水?用 RRF + Reranker 重排,效果翻倍提升!
java·后端·llm
SimonKing6 分钟前
吊打面试官系列:Spring为什么不推荐使用字段依赖注入?
java·后端·架构
魔镜魔镜_谁是世界上最漂亮的小仙女13 分钟前
java-集合
java·后端·程序员
真实的菜14 分钟前
消息队列高级特性与原理:解锁分布式系统的底层逻辑
java
若水不如远方16 分钟前
java范型
java
凌辰揽月18 分钟前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
lifallen24 分钟前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
长安不见26 分钟前
背景知识: 理解LimitLatch背后的AQS
java
小吕学编程28 分钟前
策略模式实战:Spring中动态选择商品处理策略的实现
java·开发语言·设计模式
weixin_4383354034 分钟前
Spring Boot实现接口时间戳鉴权
java·spring boot·后端