HttpClient4.5.6 设置代理以及代理验证(用户名和密码)可以访问 https
代理:
https://dandelioncloud.cn/article/details/1438773245599354882
RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); httpGet.setConfig(config);
可以访问 https:
// 设置认证
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials("username", "password"));
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); httpGet.setConfig(config);
可以访问 https:
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
// don't check
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
// don't check
}
}
};
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance("TLS");
ctx.init(null, trustAllCerts, null);
} catch (Exception e) {
e.printStackTrace();
}
LayeredConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(ctx);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslSocketFactory)
.setDefaultCredentialsProvider(provider)
.build();