IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Java :请求Https地址,忽略证书

    superadmin发表于 2024-11-29 09:33:31
    love 0

    以下以Nutz代码为例:

    Header header = Header.create();
            header.addv("Content-Type", "application/json");
            header.addv("AppCode", appcode);
            Request request = Request.create(url, Request.METHOD.POST);
            NutMap body = NutMap.NEW();
            body.put("kssj", kssj);
            body.put("jssj", jssj);
            request.setHeader(header);
            request.setData(Json.toJson(body));
            Sender sender = Sender.create(request).setTimeout(30 * 1000);
            if (url.startsWith("https")) {
                try {
                    SSLContext sslcontext = this.createIgnoreVerifySSL();
                    sender.setSSLSocketFactory(sslcontext.getSocketFactory());
                    sender.setHostnameVerifier((urlHostName, session) -> true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Response response = sender.send();
            if (response.isOK()) {
                //todo
            }

    private static class TrustAllManager
    implements X509TrustManager {
    public X509Certificate[] getAcceptedIssuers() {
    return null;
    }

    public void checkServerTrusted(X509Certificate[] certs,
    String authType) {
    }

    public void checkClientTrusted(X509Certificate[] certs,
    String authType) {
    }
    }


    public SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, new TrustManager[]{new CityLifelineServer.TrustAllManager()}, null);
    return sc;
    }



沪ICP备19023445号-2号
友情链接