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

}

相关推荐

  1. https方式请求 过滤证书

    2024-02-02 06:30:03       61 阅读
  2. Https post 请求时绕过证书验证方案

    2024-02-02 06:30:03       25 阅读
  3. HTTP请求方法

    2024-02-02 06:30:03       52 阅读
  4. HTTP有哪些请求方式

    2024-02-02 06:30:03       27 阅读
  5. HTTPS 请求中的证书验证详解(Python版)

    2024-02-02 06:30:03       65 阅读
  6. okHttp的https请求忽略ssl证书认证

    2024-02-02 06:30:03       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-02 06:30:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-02 06:30:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-02 06:30:03       82 阅读
  4. Python语言-面向对象

    2024-02-02 06:30:03       91 阅读

热门阅读

  1. advPython-4

    2024-02-02 06:30:03       49 阅读
  2. Kafka

    2024-02-02 06:30:03       53 阅读
  3. MongoDB数据库自动备份脚本

    2024-02-02 06:30:03       40 阅读
  4. 【灵活设置PostgreSQL的PROMPT1客户端提示符】

    2024-02-02 06:30:03       63 阅读
  5. Less与Sass

    2024-02-02 06:30:03       48 阅读
  6. 【PyQt6】朗读小说西游记

    2024-02-02 06:30:03       55 阅读
  7. 密码输入检测

    2024-02-02 06:30:03       55 阅读
  8. 放苹果#洛谷#dfs#c语言

    2024-02-02 06:30:03       47 阅读
  9. 异步解耦之RabbitMQ(一)

    2024-02-02 06:30:03       62 阅读
  10. RabbitMQ

    RabbitMQ

    2024-02-02 06:30:03      43 阅读