HttpClient访问https
帮忙看下我这段配置,为什么不能访问https的接口
static {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
SSLContext sc;
try {
sc = SSLContext.getInstance("TLS");
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager(){
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
} };
sc.init(null, trustAllCerts, null);
} catch (Exception e) {
LOG.error("some thing happen", e);
throw new ServiceException(e);
}
ConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sc, (s, sslSession) -> true);
ConnectionKeepAliveStrategy connectionKeepAliveStrategy = (httpResponse, httpContext) -> {
return 20L * 1000;
};
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", plainsf).register("ftp", plainsf).register("https", sslsf).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
connManager.setMaxTotal(400);
connManager.setDefaultMaxPerRoute(200);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(HTTP_CONNECT_TIME_OUT)
.setConnectionRequestTimeout(3000).setSocketTimeout(HTTP_TIME_OUT).build();
client = HttpClients.custom().setConnectionManager(connManager)
.setRetryHandler(new DefaultHttpRequestRetryHandler()).setDefaultRequestConfig(requestConfig)
.setKeepAliveStrategy(connectionKeepAliveStrategy).build();
}
static {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
SSLContext sc;
try {
sc = SSLContext.getInstance("TLS");
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager(){
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
} };
sc.init(null, trustAllCerts, null);
} catch (Exception e) {
LOG.error("some thing happen", e);
throw new ServiceException(e);
}
ConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sc, (s, sslSession) -> true);
ConnectionKeepAliveStrategy connectionKeepAliveStrategy = (httpResponse, httpContext) -> {
return 20L * 1000;
};
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", plainsf).register("ftp", plainsf).register("https", sslsf).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
connManager.setMaxTotal(400);
connManager.setDefaultMaxPerRoute(200);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(HTTP_CONNECT_TIME_OUT)
.setConnectionRequestTimeout(3000).setSocketTimeout(HTTP_TIME_OUT).build();
client = HttpClients.custom().setConnectionManager(connManager)
.setRetryHandler(new DefaultHttpRequestRetryHandler()).setDefaultRequestConfig(requestConfig)
.setKeepAliveStrategy(connectionKeepAliveStrategy).build();
}
...全文
相关推荐
httpclient来进行https访问
httpclient来进行https访问
HttpClient 使用证书访问https站点
使用HttpClient访问https 站点时,如果JRE或者JDK没有导入某个站点的证书,则会报如下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification
使用httpClient访问https+443端口号。
NULL 博文链接:https://from-null.iteye.com/blog/859795
httpclient4.1访问https的配置方法
利用tomcat搭建支持ssl的服务,并由httpClient进行https访问。整个过程简单明了。 1.生成key. 2.配置tomcat支持ssl 3.利用一个简单例子用httpClient连接ssl
httpclient访问需要客户端认证数字证书的https接口的处理方法
NULL 博文链接:https://liudeh-009.iteye.com/blog/1181571


Web 开发
