https请求出现Receivedfatalalert:handshake_failure异常解决

更新时间:2023-07-13 03:29:54 阅读: 评论:0

https请求出现Receivedfatalalert:handshake_failure异常解决
1.简述
  使⽤Https请求知道链接时出现javax.ssl.SSLHandshakeException: Received fatal alert: handshake_failure异常,是因为ssl协议错误。摩羯双子
2.解决⽅案
  主要是在创建SSLContext的时候指定TLS协议,就可以解决这个问题,使⽤的是httpclient-4.5.jar、httpcore-4.4.1.jar。
  出现异常⽰例代码如下:
import CertificateException;
河南博物馆import X509Certificate;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRespon;
import org.apache.fig.RequestConfig;
import org.apache.hods.HttpGet;
import org.fig.Registry;
import org.fig.RegistryBuilder;
import org.socket.ConnectionSocketFactory;
import org.socket.PlainConnectionSocketFactory;
import org.ssl.NoopHostnameVerifier;
import org.ssl.SSLConnectionSocketFactory;
import org.ssl.TrustStrategy;
茁壮成长什么意思import org.apache.http.impl.client.CloableHttpClient;
import org.apache.http.impl.client.HttpClients;
雪景古诗
import org.apache.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;周批改
import org.apache.http.util.EntityUtils;
public class Demo {
public static void main(String[] args) throws Exception {
String url = "/weather/today/l/22.69,113.91?par=google";
String result = "";
CloableHttpClient httpClient = null;
try {
SSLContextBuilder builder = new SSLContextBuilder();
// 全部信任不做⾝份鉴定
builder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
});
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), new String[] { "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2" }, null, NoopHostnameVerifier.INSTANCE);            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create().register("http", new PlainConnectionSocketFactory()).register("https", sslsf).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
cm.tMaxTotal(200);// max connection
httpClient = HttpClients.custom().tSSLSocketFactory(sslsf).tConnectionManager(cm).tConnectionManagerShared(true).build();
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom().tSocketTimeout(30000).tConnectTimeout(30000).build();// 设置请求和传输超时时间
httpGet.tConfig(requestConfig);
HttpRespon httpRespon = ute(httpGet);
HttpEntity resEntity = Entity();
result = String(resEntity);
} catch (Exception e) {
throw e;
} finally {
if (httpClient != null) {语文阅读理解
httpClient.clo();
}
}
System.out.println(result);
}
}
View Code
  第⼀种成功获取的⽰例代码如下:
import java.io.IOException;
import java.URI;
import javax.ssl.SSLContext;
import org.apache.fig.RequestConfig;
import org.apache.hods.CloableHttpRespon;
import org.apache.hods.HttpGet;
import org.fig.Registry;
import org.fig.RegistryBuilder;
import org.socket.ConnectionSocketFactory;
import org.socket.PlainConnectionSocketFactory;
import org.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloableHttpClient;
import org.apache.http.impl.client.HttpClients;
烧海参的家常做法import org.apache.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
public class Demo {
public static void main(String[] args) throws Exception {
String url = "/weather/today/l/22.69,113.91?par=google";
String result = null;
HttpGet get = new HttpGet();
CloableHttpRespon res = null;
CloableHttpClient client = null;
try {
RequestConfig config = RequestConfig.custom().tConnectTimeout(10000).tSocketTimeout(12000).build();
SSLContext sslContext = Instance("TLSv1.2");
sslContext.init(null,null,null);
SSLContext.tDefault(sslContext);
Registry<ConnectionSocketFactory> socketFactoryRegistry =
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(sslContext)).build();
PoolingHttpClientConnectionManager mananger = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
mananger.tMaxTotal(100);
mananger.tDefaultMaxPerRoute(20);
client = HttpClients.custom().tConnectionManager(mananger).build();
get.tConfig(config);
get.tURI(new URI(url));
res = ute(get);
result = Entity());
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
res.clo();
client.clo();
} catch (IOException e1) {
e1.printStackTrace();
}
}
System.out.println(result);
}
}
View Code
  第⼆种成功获取的⽰例代码如下:
import CertificateException;
import X509Certificate;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRespon;
import org.apache.fig.RequestConfig;
import org.apache.hods.HttpGet;
知行合一作文import org.fig.Registry;
import org.fig.RegistryBuilder;
import org.socket.ConnectionSocketFactory;
import org.socket.PlainConnectionSocketFactory;
import org.ssl.NoopHostnameVerifier;
import org.ssl.SSLConnectionSocketFactory;
import org.ssl.TrustStrategy;
import org.apache.http.impl.client.CloableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
public class Demo {
public static void main(String[] args) throws Exception {
String url = "/weather/today/l/22.69,113.91?par=google";
String result = "";
CloableHttpClient httpClient = null;
try {
SSLContextBuilder builder = new SSLContextBuilder();
// 全部信任不做⾝份鉴定
builder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
});
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), new String[] { "TLSv1", "TLSv1.2" }, null, NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create().register("http", new PlainConnectionSocketFactory()).register("https", sslsf).build();            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
cm.tMaxTotal(200);// max connection
httpClient = HttpClients.custom().tSSLSocketFactory(sslsf).tConnectionManager(cm).tConnectionManagerShared(true).build();
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom().tSocketTimeout(30000).tConnectTimeout(30000).build();// 设置请求和传输超时时间
httpGet.tConfig(requestConfig);
HttpRespon httpRespon = ute(httpGet);
HttpEntity resEntity = Entity();
result = String(resEntity);
} catch (Exception e) {
throw e;
} finally {
if (httpClient != null) {
httpClient.clo();
}
}
System.out.println(result);
}
}
View Code
  第⼆种成功是因为去除了SSLv2Hello、SSLv3协议。
  注意:三个⽰例中的最后⼀个输出记得屏蔽,因为内容较多不屏蔽则会卡⼀会。

本文发布于:2023-07-13 03:29:54,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1079344.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:请求   协议   屏蔽   时候   指定   传输   记得
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图