改变世界的是这样一群人,他们寻找梦想中的乐园,当他们找不到时,他们亲手创造了它

java为https请求指定协议版本

问题:you are using TLS 1.0/1.1 which is deprecated, please use TLS 1.2 or higher.

java应用的TLS需要升级,应用开发者需要检查所用到的TLS库,确保都升级到TLS 1.2及以上。鉴于安全问题不能忽视,请开发者尽快完成升级,以保护企业的数据安全。

 

代码:

package com.jeecg.util.tool;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;

public class OpenApiHttpUtils {
    public static String doPostJson(String apiUrl, String jsonParams, String appKey, String appSecret) {
        StringBuffer sbResult = new StringBuffer();
        try {
            //为https请求指定协议版本
            System.setProperty("https.protocols", "TLSv1.2,TLSv1.1,SSLv3");
            URL url = new URL(apiUrl);

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setDoOutput(true);

            conn.setDoInput(true);

            conn.setUseCaches(false);

            conn.setRequestMethod("POST");

            conn.setRequestProperty("Connection", "Keep-Alive");

            conn.setRequestProperty("Charset", "UTF-8");

            byte[] data = jsonParams.getBytes("UTF-8");

            conn.setRequestProperty("Content-Length", String.valueOf(data.length));

            conn.setRequestProperty("Content-Type", "application/json");

            conn.setRequestProperty("app-key", appKey);
            conn.setRequestProperty("req-time", String.valueOf(System.currentTimeMillis()));
            conn.setRequestProperty("sign", MD5(jsonParams + "_" + appSecret, "UTF-8"));
            conn.connect();
            OutputStream out = new DataOutputStream(conn.getOutputStream());

            out.write(data);
            out.flush();
            out.close();

            if (200 == conn.getResponseCode()) {
                InputStream inputStream = conn.getInputStream();
                try {
                    String readLine = null;
                    BufferedReader responseReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                    while ((readLine = responseReader.readLine()) != null) {
                        sbResult.append(readLine).append("\n");
                    }
                    responseReader.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sbResult.toString();
    }

    private static String MD5(String str, String charset) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes(charset));
        byte[] result = md.digest();
        StringBuffer sb = new StringBuffer(32);
        for (int i = 0; i < result.length; i++) {
            int val = result[i] & 0xFF;
            if (val <= 15) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(val));
        }
        return sb.toString().toLowerCase();
    }
}

 

posted @ 2025-04-10 15:56  水狼一族  阅读(201)  评论(0)    收藏  举报
改变世界的是这样一群人,他们寻找梦想中的乐园,当他们找不到时,他们亲手创造了它