首页 > 编程学习 > Java :请求Https地址,忽略证书
202411 月29

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

以下以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;
}

本文地址:https://wizzer.cn/archives/3839 , 转载请保留.

本文目前尚无任何评论.

发表评论