• 网络-httpclient调用https服务端绕过证书的方法


    httpclient调用https服务端绕过证书的方法

    在日常开发或者测试中,通常会遇到需要用httpclient客户端调用对方http是服务器的场景,由于没有证书,所以直接是无法调用的。采用下面的方法可以绕过证书验证:

    TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
    
                public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                }
    
                public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                }
            }};
            // 创建一个SSL上下文,设置信任管理器
            try {
                SSLContext sc = SSLContext.getInstance("TLS");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            } catch (Exception e) {
                e.printStackTrace();
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    完整代码:

     public static String sendHttpPostRequest(String url, JSONObject param) throws Exception {
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                }
                public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                }
            }};
            // 创建一个SSL上下文,设置信任管理器
            try {
                SSLContext sc = SSLContext.getInstance("TLS");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            } catch (Exception e) {
                e.printStackTrace();
            }
            URL apiUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
            // 设置请求方法为 POST
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            // 添加请求头
            connection.setRequestProperty("Content-Type", "application/json");
            //connection.setRequestProperty("", accessToken);
            // 发送 POST 请求必须设置如下两行
            OutputStream outputStream = connection.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
            outputStreamWriter.write(param.toJSONString());
            outputStreamWriter.flush();
            outputStreamWriter.close();
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);
            // 创建一个 BufferedReader 来读取服务器的响应
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuffer response = new StringBuffer();
            String inputLine;
            while ((inputLine = bufferedReader.readLine()) != null) {
                response.append(inputLine);
            }
            bufferedReader.close();
            System.out.println("Response: " + response.toString());
            return null;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
  • 相关阅读:
    【JavaSE】初识数据类型
    一文带你学会CSS选择器
    uni-app:实现图片周围的图片按照圆进行展示
    C++征途 --- Vector容器
    Vue 前端代码风格指南
    金仓数据库 KingbaseES V8.3 至 V8.6 迁移最佳实践(4. V8.3 到 V8.6 数据库移植实战)
    二、nginx URL重写[rewrite]
    opencv案例06-基于opencv图像匹配的消防通道障碍物检测与深度yolo检测的对比
    WIN11系统设置重启与睡眠唤醒后自动拨号
    通过Power Platform自定义D365 CE 业务需求 - 5. 使用Power Virtual Agent
  • 原文地址:https://blog.csdn.net/weixin_37842058/article/details/136432372