首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >okhttp3 to setSslSocketFactory,setWriteTimeout和setReadTimeout

okhttp3 to setSslSocketFactory,setWriteTimeout和setReadTimeout
EN

Stack Overflow用户
提问于 2022-07-20 16:13:49
回答 1查看 64关注 0票数 0

又是我,

有人帮我改了这个,

代码语言:javascript
运行
复制
    OkHttpClient client() {
        if (client != null)
            return client;
        if (System.getenv("trustSelfSignedCert") != null
                && System.getenv("trustSelfSignedCert").equalsIgnoreCase("true"))
            trustSelfSigned = true;
        if (System.getProperty("trustSelfSignedCert") != null
                && System.getProperty("trustSelfSignedCert").equalsIgnoreCase("true"))
            trustSelfSigned = true;
        OkHttpClient client0 = new OkHttpClient();
//        if (proxy)
//            client0.setProxy(new Proxy(Type.HTTP, new InetSocketAddress("proxy.domain.com", 8080)));
        if (trustSelfSigned) {
            TrustManager[] certs = new TrustManager[] { new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
            } };
            SSLContext ctx = null;
            try {
                ctx = SSLContext.getInstance(tls);
                ctx.init(null, certs, new SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
                client0.setSslSocketFactory(ctx.getSocketFactory());
            } catch (java.security.GeneralSecurityException ex) {
            }
        }
        client0.setWriteTimeout(timeout * 1000L, TimeUnit.MILLISECONDS);
        client0.setReadTimeout(timeout * 1000L, TimeUnit.MILLISECONDS);
        return client = client0;
    }    

为了这个,

代码语言:javascript
运行
复制
    OkHttpClient client() {
        if (client != null)
            return client;
        if (System.getenv("trustSelfSignedCert") != null
                && System.getenv("trustSelfSignedCert").equalsIgnoreCase("true"))
            trustSelfSigned = true;
        if (System.getProperty("trustSelfSignedCert") != null
                && System.getProperty("trustSelfSignedCert").equalsIgnoreCase("true"))
            trustSelfSigned = true;
        SSLContext ctx = null;
        if (trustSelfSigned) {
            TrustManager[] certs = new TrustManager[] { new X509TrustManager() {
                private X509TrustManager standardTrustManager = null;

                @Override
                public X509Certificate[] getAcceptedIssuers() { 
                    return new X509Certificate[]{}; 
                }

                @Override
                public void checkServerTrusted(X509Certificate[] certificates, String authType)
                        throws CertificateException {
                    if ((certificates != null) && (certificates.length == 1)) {
                        certificates[0].checkValidity();
                    } else {
                        standardTrustManager.checkServerTrusted(certificates, authType);
                    }
                }

                @Override
                public void checkClientTrusted(X509Certificate[] certificates, String authType)
                        throws CertificateException {
                    standardTrustManager.checkClientTrusted(certificates, authType);
                }
            } };            
            try {
                ctx = SSLContext.getInstance(tls);
                ctx.init(null, certs, new SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
            } catch (java.security.GeneralSecurityException ex) {
            }
        }        
        if (proxy) {
            Proxy proxY = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.domain.com", 8080)); 
            OkHttpClient client0 = new OkHttpClient.Builder().proxy(proxY)
                    .sslSocketFactory(ctx.getSocketFactory())
                    .writeTimeout(timeout * 1000L, TimeUnit.MILLISECONDS)
                    .readTimeout(timeout * 1000L, TimeUnit.MILLISECONDS)
                    .build();
            return client = client0;
        } else {
            OkHttpClient client0 = new OkHttpClient.Builder()
                    .sslSocketFactory(ctx.getSocketFactory())
                    .writeTimeout(timeout * 1000L, TimeUnit.MILLISECONDS)
                    .readTimeout(timeout * 1000L, TimeUnit.MILLISECONDS)
                    .build();   
            return client = client0;
        }
        
    }

不幸的是,当我知道这个程序在哪里运行时,这种转换是错误的,因为在我之前,我只想修复这个漏洞。代理实际上可以在jvm参数上完成,所以我只是将其注释掉,或者您也可以将它添加到新代码中。问题在于setSslSocketFactory、setWriteTimeout和setReadTimeout。

如果我运行新代码,它就会出错,

代码语言:javascript
运行
复制
javax.net.ssl.SSLException: java.lang.NullPointerException

希望有人会看到这段代码已经修复了类似的问题。再次感谢。

EN

回答 1

Stack Overflow用户

发布于 2022-07-22 21:10:27

这些都需要注释掉,这是一个错误。

代码语言:javascript
运行
复制
private X509TrustManager standardTrustManager = null;
standardTrustManager.checkServerTrusted(certificates, authType);

Then it works!
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73054942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档