org.apache.http.conn.ssl.X509HostnameVerifier错误解决

解决Jersey与HTTP客户端依赖问题
本文介绍了如何解决在使用Jersey客户端时遇到的ClassNotFoundException和NoClassDefFoundError错误。通过添加resteasy-client和httpclient依赖解决了JerseyClientBuilder和X509HostnameVerifier的问题,并提供了针对HttpParams错误的解决方案。

最初的错误是:

java.lang.RuntimeException: java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder

添加以下依赖项解决了初始错误

   <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-client</artifactId>
        <scope>provided</scope>
    </dependency>

然后我们收到了第二个错误:

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

添加以下依赖项解决了第二个(X509HostnameVerifier)错误:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <scope>provided</scope>
    </dependency>

如果还有这样的错误:

java.lang.ClassNotFoundException: org.apache.http.params.HttpParams

还需要依赖:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>版本号</version>
</dependency>
package com.hw.test.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.util.EntityUtils; import javax.net.ssl.SSLContext; import java.io.File; import java.io.IOException; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.util.Map; public class HttpUtils { public static HttpClient createSSLInsecureClient() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { SSLContext sslContext = new SSLContextBuilder() .loadTrustMaterial(null, (x509CertChain, authType) -> true) .build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslContext, NoopHostnameVerifier.INSTANCE); return HttpClients.custom() .setSSLSocketFactory(sslsf) .build(); } public static String sendGetRequest(String url) throws IOException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException { HttpClient httpClient = createSSLInsecureClient(); HttpGet httpGet = new HttpGet(url); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } public static String sendPostRequest(String url,Map<String,String> heads, String jsonBody){ HttpClient httpClient = null; try { httpClient = createSSLInsecureClient(); HttpPost httpPost = new HttpPost(url); if (heads != null && !heads.isEmpty()) { for (String key : heads.keySet()) { httpPost.setHeader(key, heads.get(key)); } } httpPost.setHeader("Content-Type", "application/json;charset=utf-8"); httpPost.setEntity(new StringEntity(jsonBody, ContentType.APPLICATION_JSON)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); return EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } } public static String uploadImage(String url, File imageFile) throws IOException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException { HttpClient httpClient = createSSLInsecureClient(); HttpPost httpPost = new HttpPost(url); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addBinaryBody("image", imageFile, ContentType.IMAGE_JPEG, imageFile.getName()); HttpEntity multipart = builder.build(); httpPost.setEntity(multipart); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } } 什么问题,url是https
最新发布
12-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值