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

目录
相关文章
|
网络安全 Python
There was a problem confirming the ssl certificate
There was a problem confirming the ssl certificate
295 0
Glide Caused by: javax.net.ssl.SSLPeerUnverifiedException: Hostname not verified:
Glide Caused by: javax.net.ssl.SSLPeerUnverifiedException: Hostname not verified:
|
15天前
|
存储 网络安全
Curl error (60): SSL peer certificate or SSH remote key was not OK for https://update.cs2c.com.cn/NS/V10/V10SP2/os/adv/lic/base/x86_64/repodata/repomd.xml [SSL: no alternative certificate subject name matches target host name 'update.cs2c.com.cn']
【10月更文挑战第30天】在尝试从麒麟软件仓库(ks10-adv-os)下载元数据时,遇到 SSL 证书验证问题。错误提示为:`Curl error (60): SSL peer certificate or SSH remote key was not OK`。可能原因包括证书不被信任、证书与域名不匹配或网络问题。解决方法包括检查网络连接、导入 SSL 证书、禁用 SSL 证书验证(不推荐)、联系仓库管理员、检查系统时间和尝试其他镜像。
42 1
|
5月前
|
Shell 网络安全 开发工具
【已解决】SSL certificate problem: self signed certificate
SSL certificate problem: self signed certificate
232 2
|
网络安全 PHP
cURL error 60: SSL certificate problem: self signed certificate in certificate
一、问题 cURL error 60: SSL certificate problem: self signed certificate in certificate chain - 服务器http302 出现这个一般是自签名证书的问题
3084 0
|
3月前
|
Linux 网络安全 API
【Azure 环境】当在Azure 环境中调用外部接口不通时,如何定位SSL Certificate Problem
【Azure 环境】当在Azure 环境中调用外部接口不通时,如何定位SSL Certificate Problem
|
安全 网络安全 PHP
解决SSL routines:ssl3_get_server_certificate:certificate verify failed
解决SSL routines:ssl3_get_server_certificate:certificate verify failed
677 0
|
网络安全
【IntelliJ IDEA】连接https报错问题: E230001: Server SSL certificate verification failed:
最近新安装了一个2018版本的idea,但是用idea去连接SVN的时候出现了这样的错误,在网上搜了很多,都没有用,最后找到这样的方式,把问题搞定,果断把解决方式记录下,以便帮助更多的人。
394 1
【IntelliJ IDEA】连接https报错问题: E230001: Server SSL certificate verification failed:
|
网络安全 开发工具 git
git SSL certificate problem: unable to get local issuer certificate
git SSL certificate problem: unable to get local issuer certificate
332 1
|
网络安全
ssl.SSLError [SSL CERTIFICATE_VERIFY_FAILED] certificate verify failed
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
232 0