美文网首页
Java中httpclient.jar和httpcore.jar

Java中httpclient.jar和httpcore.jar

作者: 零点145 | 来源:发表于2019-06-12 14:29 被阅读0次

前言说明:当前我的工程中使用的httpclient-4.3.6.jar和httpcore-4.3.3.jar版本的包。

<dependency>

    <groupId>org.apache.httpcomponents</groupId>

    <artifactId>httpcore</artifactId>

    <version>4.3.3</version>

</dependency>

<dependency>

    <groupId>org.apache.httpcomponents</groupId>

    <artifactId>httpclient</artifactId>

    <version>4.3.6</version>

</dependency> 

今天执行阿里云的OSS相关操作时,首先提示以下异常:

java.lang.ClassNotFoundException: org.apache.http.ssl.TrustStrategy

详细如下:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/ssl/TrustStrategy

    at com.aliyun.oss.OSSClient.<init>(OSSClient.java:246)

    at com.aliyun.oss.OSSClient.<init>(OSSClient.java:206)

    at com.autonavi.oss.client.DefaultOSSClient.<clinit>(DefaultOSSClient.java:12)

    at com.autonavi.oss.put.Upload.put(Upload.java:20)

    at com.autonavi.oss.put.Upload.main(Upload.java:58)

Caused by: java.lang.ClassNotFoundException: org.apache.http.ssl.TrustStrategy

    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)

    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)

    at java.security.AccessController.doPrivileged(Native Method)

    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)

    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

    ... 5 more

从上面报错提示可以看出,是org/apache/http/ssl/TrustStrategy下面找不到TrustStrategy这个类,我查了下依赖中的httpclient-4.3.6.jar和httpcore-4.3.3.jar两个包,均没有这个类。

而4.4版本中有ssl路径:

及对应org/apache/http/ssl/TrustStrategy方法。

很明显是版本不对,应该换成新版本。

<dependency>

    <groupId>org.apache.httpcomponents</groupId>

    <artifactId>httpcore</artifactId>

    <version>4.4</version>

</dependency>

重新执行代码,上面异常已经消失,不过出现新异常:

java.lang.NoClassDefFoundError: org/apache/http/conn/ssl/NoopHostnameVerifier

异常详细:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/ssl/NoopHostnameVerifier

    at com.aliyun.oss.common.comm.DefaultServiceClient.createHttpClientConnectionManager(DefaultServiceClient.java:230)

    at com.aliyun.oss.common.comm.DefaultServiceClient.<init>(DefaultServiceClient.java:78)

    at com.aliyun.oss.OSSClient.<init>(OSSClient.java:246)

    at com.aliyun.oss.OSSClient.<init>(OSSClient.java:206)

    at com.autonavi.oss.client.DefaultOSSClient.<clinit>(DefaultOSSClient.java:12)

    at com.autonavi.oss.put.Upload.put(Upload.java:20)

    at com.autonavi.oss.put.Upload.main(Upload.java:58)

Caused by: java.lang.ClassNotFoundException: org.apache.http.conn.ssl.NoopHostnameVerifier

    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)

    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)

    at java.security.AccessController.doPrivileged(Native Method)

    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)

    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

    ... 7 more

很明显是版本不对,是由于httpclient-4.3.6.jar缺少NoopHostnameVerifier方法:

而4.4版本中有这个方法:

应该换成新版本,即换成httpclient-4.4.jar

<dependency>

    <groupId>org.apache.httpcomponents</groupId>

    <artifactId>httpclient</artifactId>

    <version>4.4</version>

</dependency> 

查看下jar包中相应路径存在上面那个类。

最后再次执行,执行成功,无异常!

所以,下次碰到以下两个异常时:

java.lang.ClassNotFoundException: org.apache.http.ssl.TrustStrategy

java.lang.NoClassDefFoundError: org/apache/http/conn/ssl/NoopHostnameVerifier

需要升级httpclient.jar和httpcore.jar两个包的版本,4.3的版本中不含有上述两个类,需要同时升级到4.4版本。

即替换为httpclient-4.4.jar和httpcore-4.4.jar

写在最后:

码字不易看到最后了,那就点个关注呗,只收藏不点关注的都是在耍流氓!

关注并私信我“架构”,免费送一些Java架构资料,先到先得!

相关文章

网友评论

      本文标题:Java中httpclient.jar和httpcore.jar

      本文链接:https://www.haomeiwen.com/subject/lmclfctx.html