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协议。
注意:三个⽰例中的最后⼀个输出记得屏蔽,因为内容较多不屏蔽则会卡⼀会。