本文使用的HTTPclient版本是4.5.13
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.config.Registry;
import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.hc.core5.ssl.TrustStrategy;
public class Test {
public static void main(String[] args) throws Exception {
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", trustHttpsCertificates())
.build();
//创建一个ConnectionManager对象
PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager(registry);
//定制CloseableHttpClient对象
HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(pool);
//代理(此处填入自己的代理ip和端口)
HttpHost proxy = new HttpHost("127.0.0.1",99999);
//配置好HTTPclient之后,通过build方法来获取HTTPclient对象
CloseableHttpClient httpClient = httpClientBuilder.setProxy(proxy).build();
// 创建Get请求
HttpGet httpGet = new HttpGet("https://www.penghu-land.gov.tw:8080/query/noncityland.jsp?csrf.param=01705D516345C30DFF15959277DD2FF0&menu=true");
// 响应模型
CloseableHttpResponse response = null;
try {
// 由客户端执行(发送)Get请求
response = httpClient.execute(httpGet);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 创建支持安全协议的连接工厂,就可以访问不安全的证书了
* @return
* @throws Exception
*/
private static ConnectionSocketFactory trustHttpsCertificates() throws Exception {
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
});
SSLContext sslContext = sslContextBuilder.build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
new String[]{"SSLv2Hello","SSLv3","TLSv1","TLSv1.1","TLSv1.2"}
,null, NoopHostnameVerifier.INSTANCE);
return sslConnectionSocketFactory;
}
}
本文介绍如何利用Apache HTTPClient 5.x版本发送GET请求至HTTPS站点,并通过自定义信任策略绕过SSL证书验证。文中展示了如何创建一个支持多种安全协议的连接工厂,以实现对不安全证书的支持。
1985

被折叠的 条评论
为什么被折叠?



