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

}

相关推荐
我就是我35234 分钟前
记录一次SpringMVC的406错误
java·后端·springmvc
向哆哆36 分钟前
Java应用程序的跨平台性能优化研究
java·开发语言·性能优化
lisanndesu42 分钟前
HTTPS
网络协议·http·https
ekkcole1 小时前
windows使用命令解压jar包,替换里面的文件。并重新打包成jar包,解决Failed to get nested archive for entry
java·windows·jar
handsomestWei2 小时前
java实现多图合成mp4和视频附件下载
java·开发语言·音视频·wutool·图片合成视频·视频附件下载
全栈若城2 小时前
03 Python字符串与基础操作详解
java·开发语言·python
伯牙碎琴2 小时前
二、Spring Framework基础:IoC(控制反转)和DI(依赖注入)
java·spring·log4j
菲力蒲LY2 小时前
输入搜索、分组展示选项、下拉选取,全局跳转页,el-select 实现 —— 后端数据处理代码,抛砖引玉展思路
java·前端·mybatis
南宫生2 小时前
力扣每日一题【算法学习day.130】
java·学习·算法·leetcode
!!!5252 小时前
Java实现斗地主-做牌以及对牌排序
java·算法