开发者社区> 乾初> 正文

javax.net.ssl.SSLException:hostname in certificate didn't match

简介: 终于建了一个自己个人小站:https://huangtianyu.gitee.io,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~ 现在网站用https的服务器越来越多了,在编写程序的时候往往会遇到证书验证错误。
+关注继续查看

终于建了一个自己个人小站:https://huangtianyu.gitee.io,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~

现在网站用https的服务器越来越多了,在编写程序的时候往往会遇到证书验证错误。也就是SSLException错误。下面给出解决方案:

private static DefaultHttpClient getHttpClient() {
        try {
            // 禁止https证书验证
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);
            SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);//运行所有的hostname验证

            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

            // 禁用Cookie2请求头
            HttpClientParams.setCookiePolicy(params, CookiePolicy.RFC_2109);
            HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY);
            HttpClientParams.setCookiePolicy(params, CookiePolicy.NETSCAPE);

            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));

            ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

            HttpConnectionParams.setConnectionTimeout(params, 3000);
            HttpConnectionParams.setSoTimeout(params, 5000);

            return new DefaultHttpClient(ccm, params);
        } catch (Exception e) {
            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
            HttpConnectionParams.setSoTimeout(httpParams, 5000);

            return new DefaultHttpClient(httpParams);
        }
    }
通过上述方法获取得到的DefaultHttpClient ,通过该HttpClient再次进行网络请求就能绕过证书验证。该方法需要导入Apache的网络包:org.apache.http.legacy.jar

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Glide Caused by: javax.net.ssl.SSLPeerUnverifiedException: Hostname not verified:
Glide Caused by: javax.net.ssl.SSLPeerUnverifiedException: Hostname not verified:
52 0
Fetch failed: unable to access': E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
Fetch failed: unable to access': E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
128 0
java https 请求 javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
java https 请求 javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
9358 0
The handler does not support client authentication certificates with this combination of libcurl (7.54.0) and its SSL backend ("LibreSSL/2.0.20")
NET Core的跨平台大家已经有目共睹,而在MAC平台上做开发已经成为目前的主流,无论哪种语言。 在一次微服务移植的过程中,客户端需要发送Http自定义混合验证,在MonoNET上没有任何问题,而移植到NET Core 2.0并运行,就出现了错误:The handler does not support client authentication certificates with this combination of libcurl (7.54.0) and its SSL backend ("LibreSSL/2.0.20")。
1070 0
hadoop_异常_01_ Unable to determine address of the host-falling back to "localhost" address java.net.UnknownHostException: rayner
一、异常现象 安装好hadoop之后,执行格式化namenode命令时,抛出以下异常: 18/03/21 21:02:10 WARN net.DNS: Unable to determine local hostname -falling back to "localhost" java.
1516 0
Server asks us to fall back to SIMPLE auth, but this client is configured to only allow secure connections.
我是在flume向hdfs 写(sink)数据时遇到的这个错误. Server (是指hdfs) asks us to fall back to SIMPLE auth, but this client (是指flume) is configured to only allow secure (是指kerberos) connections.
3224 0
Zeus Web Server 'SSL2_CLIENT_HELLO' Remote Buffer Overflow Vulnerability
http://www.securityfocus.com/data/vulnerabilities/exploits/37829.
548 0
Multiple Vendor TLS Protocol Session Renegotiation Security Vulnerability
http://www.securityfocus.com/data/vulnerabilities/exploits/36935-2.
609 0
+关注
乾初
Stay foolish,Stay hungry.
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载