Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 26984|回复: 0

OkHttp3出现java.io.IOException: Hostname was not verified解决方案

[复制链接]
  • TA的每日心情
    奋斗
    2024-11-24 15:47
  • 签到天数: 804 天

    [LV.10]以坛为家III

    2053

    主题

    2111

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    726782
    发表于 2021-4-6 15:46:20 | 显示全部楼层 |阅读模式

    问题

    用OkHttp3做https请求时候报了个java.io.IOException: Hostname was not verified的错误。

     

    问题分析

    通常是因为SSL协议握手的过程中,这个服务度地址的证书没有被证实,被信任。

    报错消息如下。

      Hostname a.com not verified:
        certificate: sha256/s+ZoW0wxlNvmDUguAjYVvc6xxnIetO4XUMissqHkBPg=
        DN: CN=*.b.com, OU=Domain Control Validated
        subjectAltNames: [*.b.com, b.com]

    可以看到请求的证书的域名为b.com,而我们要请求的是a.com,因此错误原因是在验证证书时发现真正请求的域名和服务器的证书域名不一致。

    解决方案

    如果你认为运行的证书没有任何意义,并且想要绕过它们,那么就需要添加一个空主机名验证程序以使后面的请求正常工作。具体代码如下

       public static OkHttpClient.Builder ignoreSSL (OkHttpClient.Builder builder) {
            builder.sslSocketFactory(createSSLSocketFactory())
                .hostnameVerifier((s, sslSession) -> true);
            return builder;
        }
    
        private static SSLSocketFactory createSSLSocketFactory () {
    
            SSLSocketFactory sSLSocketFactory = null;
    
            try {
                SSLContext sc = SSLContext.getInstance("TLS");
                sc.init(null, new TrustManager[]{new TrustAllManager()}, new SecureRandom());
                sSLSocketFactory = sc.getSocketFactory();
            } catch (Exception e) {
                LOGGER.info(e.getMessage(), e);
            }
    
            return sSLSocketFactory;
        }
    
        private static class TrustAllManager implements X509TrustManager {
    
            @Override
            public void checkClientTrusted (java.security.cert.X509Certificate[] x509Certificates,
                String s) throws java.security.cert.CertificateException {
    
            }
    
            @Override
            public void checkServerTrusted (java.security.cert.X509Certificate[] x509Certificates,
                String s) throws java.security.cert.CertificateException {
    
            }
    
    
            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers () {
                return new X509Certificate[0];
            }
        }

    自己封装的http工具类

    我自己封装的完整的OkHttp3Util工具类代码如下。

    import com.google.common.base.Joiner;
    import com.google.common.collect.Lists;
    import lombok.extern.slf4j.Slf4j;
    import okhttp3.*;
    import org.apache.commons.collections.MapUtils;
    import org.apache.http.util.TextUtils;
    
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSocketFactory;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.security.SecureRandom;
    import java.security.cert.X509Certificate;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @author magotzis on 2018/7/31 下午5:06
     */
    @Slf4j
    public class HttpsUtil {
    
        private static final OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .connectTimeout(10, TimeUnit.SECONDS)
                .sslSocketFactory(createSSLSocketFactory())
                .hostnameVerifier((s, sslSession) -> true)
                .build();
    
    
        private static final Joiner AMPERSAND_JOINER = Joiner.on("&");
    
        // 工具类不需要实例化
        private HttpsUtil() {
        }
    
        private static SSLSocketFactory createSSLSocketFactory () {
    
            SSLSocketFactory sSLSocketFactory = null;
    
            try {
                SSLContext sc = SSLContext.getInstance("TLS");
                sc.init(null, new TrustManager[]{new TrustAllManager()}, new SecureRandom());
                sSLSocketFactory = sc.getSocketFactory();
            } catch (Exception e) {
                log.info(e.getMessage(), e);
            }
    
            return sSLSocketFactory;
        }
    
        private static class TrustAllManager implements X509TrustManager {
    
            @Override
            public void checkClientTrusted (java.security.cert.X509Certificate[] x509Certificates,
                                            String s) throws java.security.cert.CertificateException {
            }
    
            @Override
            public void checkServerTrusted (java.security.cert.X509Certificate[] x509Certificates,
                                            String s) throws java.security.cert.CertificateException {
            }
    
    
            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers () {
                return new X509Certificate[0];
            }
        }
    
    
        /**
         * 异步调用get请求
         *
         * @param url      请求的url
         * @param callback 回调处理
         */
        public static void getByAsyn(String url, Callback callback) {
            Request request = new Request.Builder()
                    .get()
                    .url(url)
                    .build();
    
            client.newCall(request).enqueue(callback);
        }
    
        /**
         * 同步调用get请求
         *
         * @param url 请求的url
         * @return response信息
         * @throws IOException IO异常
         */
        public static Response getBySync(String url) throws IOException {
            Request request = new Request.Builder()
                    .get()
                    .url(url)
                    .build();
            return client.newCall(request).execute();
        }
    
        /**
         * post请求
         *
         * @param url         请求的url
         * @param requestBody requestBody信息
         * @param headers     请求头部
         * @return response信息
         * @throws IOException IO异常
         */
        public static Response post(String url, RequestBody requestBody, Headers headers) throws IOException {
            Request.Builder build = new Request.Builder();
            if (headers != null) {
                build.headers(headers);
            }
            Request request = build
                    .post(requestBody)
                    .url(url)
                    .build();
            return client.newCall(request).execute();
        }
    
        /**
         * 拼接url和param
         *
         * @param url    请求的url
         * @param params 需要带的参数
         * @return 拼接好的url
         */
        public static String buildUrl(String url, Map<String, String> params) {
            if (MapUtils.isEmpty(params)) {
                return url;
            }
            List<String> nameValueList = Lists.newArrayListWithExpectedSize(params.size());
            for (Map.Entry<String, String> entry : params.entrySet()) {
                nameValueList.add(entry.getKey() + "=" + entry.getValue());
            }
    
            return url + "?" + AMPERSAND_JOINER.join(nameValueList);
    
        }
    
        /**
         * @param url 下载链接
         * @return 文件路径
         */
        public static File download(String url) {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            String fileName;
            File file = null;
            try {
                Response response = client.newCall(request).execute();
                // 解析文件名,如果不存在则随机生成一个uuid作为文件名
                fileName = getHeaderFileName(response);
                if (TextUtils.isEmpty(fileName)) {
                    fileName = UidUtil.getUid();
                }
                file = new File(fileName);
                // 输出保存文件
                byte[] buf = new byte[2048];
                int len;
                try (InputStream is = Objects.requireNonNull(response.body()).byteStream();
                     FileOutputStream fos = new FileOutputStream(file)) {
                    while ((len = is.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                    }
                    fos.flush();
                }
            } catch (IOException e) {
                log.error("下载文件失败", e);
            }
            return file;
        }
    
        /**
         * 解析文件头
         * Content-Disposition: inline; filename="test.pdf";
         * filename*=UTF-8''test.pdf
         */
        private static String getHeaderFileName(Response response) {
            String header = response.header("Content-Disposition");
            if (!TextUtils.isEmpty(header)) {
                header = header.replace("inline;filename=", "");
                header = header.replace("filename*=utf-8", "");
                String[] strings = header.split("; ");
                if (strings.length > 1) {
                    header = strings[1].replace("filename=", "");
                    header = header.replace("\"", "");
                    return header;
                }
                return "";
            }
            return "";
        }
    
    }

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-12-22 18:07 , Processed in 0.071230 second(s), 30 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表