1. httpclient 4.2.3
1.1 download url
http://archive.apache.org/dist/httpcomponents/httpclient/binary/
1.2 依赖 jar
commons-logging-1.1.1.jar
httpclient-4.2.3.jar
httpcore-4.2.2.jar
1.3 示例代码
import java.io.IOException;
import java.net.Socket;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
public class HttpsClient {
public static void main(String[] args) throws Exception {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{new MyTrustManager()}, null);
SSLSocketFactory sslSocketFactory = new MySSLSocketFactory(sslContext);
SchemeRegistry schemeRegister = new SchemeRegistry();
schemeRegister.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
schemeRegister.register(new Scheme("https", 443, sslSocketFactory));
PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegister);
cm.setMaxTotal(200); // 所有主机的最大并发连接数
cm.setDefaultMaxPerRoute(100); // 单个主机的最大并发连接数
HttpGet get = new HttpGet("https://kyfw.12306.cn/otn/login/init");
HttpClient httpClient = new DefaultHttpClient(cm);
HttpResponse response = httpClient.execute(get);
String respStr = EntityUtils.toString(response.getEntity());
System.out.println(respStr);
}
}
class MyTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
class MySSLSocketFactory extends SSLSocketFactory {
public MySSLSocketFactory(SSLContext sslContext) {
super(sslContext, ALLOW_ALL_HOSTNAME_VERIFIER);
}
@Override
public Socket createSocket(HttpParams params) throws IOException {
Socket socket = super.createSocket(params);
if (socket instanceof SSLSocket) {
// Allow TLSv1 protocol only
((SSLSocket)socket).setEnabledProtocols(new String[]{"TLSv1"});
}
return socket;
}
}
2. httpclient 4.3.6
2.1 download url
http://archive.apache.org/dist/httpcomponents/httpclient/binary/
2.2 依赖 jar
commons-logging-1.1.3.jar
httpclient-4.3.6.jar
httpcore-4.3.3.jar
2.3 示例代码
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
public class HttpsClient {
public static void main(String[] args) throws Exception {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{new MyTrustManager()}, null);
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
sslContext, new String[]{"TLSv1"}, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> register = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(register);
cm.setMaxTotal(200); // 所有主机的最大并发连接数
cm.setDefaultMaxPerRoute(100); // 单个主机的最大并发连接数
HttpGet get = new HttpGet("https://kyfw.12306.cn/otn/login/init");
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
HttpResponse response = httpClient.execute(get);
String respStr = EntityUtils.toString(response.getEntity());
System.out.println(respStr);
}
}
class MyTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
-- END
分享到:
相关推荐
不会的可以在评论区留言哈,这是我自己做项目用到的。所以绝对可用!同时共享出来给到大家
使用HttpClient4.5实现https请求忽略SSL证书验证工具类
当访问需要客户端认证的HTTPS接口时,通常需要提供数字证书以证明客户端的身份。本篇文章将深入探讨如何使用HttpClient来处理这种情况。 HttpClient是一个强大的Java库,用于执行HTTP请求。然而,当遇到需要客户端...
根据提供的文件信息,我们可以深入探讨如何使用`httpclient`库来进行`https`访问,并了解其中涉及的关键概念和技术细节。 ### 标题与描述解析:使用`httpclient`进行`https`访问 #### 1. `httpclient`简介 `...
访问HTTPS接口时,我们通常需要创建自定义的`SSLSocketFactory`,以便加载我们的证书和信任策略。在提供的文件`MySSLProtocolSocketFactory.java`中,可能实现了这个功能。它可能会覆盖默认的SSL协议,允许我们提供...
本文将详细介绍如何使用`HttpClient`和`HttpsURLConnection`两种方式来访问HTTPS网站,包括验证证书和不验证证书的实现方法。 ### 1. Android中的HttpClient `HttpClient`是Apache提供的一种HTTP客户端库,它支持...
标题中的“使用httpClient访问https+443端口号”指的是使用Apache HttpClient库来发起HTTPS(安全超文本传输协议)请求,目标服务器的默认端口是443。HTTPS是一种基于SSL/TLS的安全通信协议,用于在客户端和服务器...
在IT行业中,网络通信安全是至关重要的,尤其是在处理HTTPS(HTTP over SSL/TLS)协议时。HTTPS通过使用SSL/TLS协议来加密传输数据,保护用户的隐私和数据安全。然而,在某些特殊情况下,如进行测试、调试或者处理自...
当涉及到HTTPS接口调用时,HttpClient可以处理加密通信,确保数据传输的安全性。在这个主题中,我们将深入探讨如何在Java中使用HttpClient来实现HTTPS接口调用。 首先,我们需要理解HTTPS协议。HTTPS是HTTP(超文本...
这个实例主要涉及如何配置HttpClient来忽略SSL(Secure Socket Layer)验证,这对于在开发和测试环境中处理自签名证书或未认证的服务器非常有用。以下将详细介绍HttpClient的使用以及如何进行SSL验证的忽略。 首先...
HttpClient之Https应用实例~ 包含: HttpClient 使用代理访问Https HttpClient 信任所有主机-对于任何证书都不做检查 HttpClient 允许所有证书的校验(包括过期证书)
HttpClient配置SSL绕过https证书实例,附件中包含所需httpclient组件jar库。博客地址:http://blog.csdn.net/irokay/article/details/78801307。
【标题】:“httpclient4.1访问https的配置方法” 【描述】:“本教程将指导你如何利用Apache HttpClient 4.1实现对HTTPS服务的访问,首先需要在Tomcat服务器上配置SSL支持。我们将涵盖SSL证书的生成、Tomcat服务器...
总结来说,HttpClient是Java中实现HTTP/HTTPS请求的强大工具,而跳过SSL证书校验的功能在开发和测试环境中非常有用。通过创建自定义的SSLContext和HostnameVerifier,我们可以轻松地绕过SSL校验,从而简化接口调用...
本篇文章将详细讲解如何在HTTPClient 4.5版本中绕过SSL(Secure Sockets Layer)认证,实现对HTTPS网站的访问。 首先,了解SSL/TLS(Transport Layer Security)协议的重要性。SSL/TLS是网络安全传输的标准,它通过...
android HttpClient访问某些Https时,出现了问题,无法访问,好像是要安全验证。此Demo解决了此问题,HttpClient能够Https和Http类型的URL了。 在eclipse下打开工程若有乱码,请把eclipse的字符编码改成UTF-8。
如果Servlet需要HTTPS(SSL/TLS)加密,HttpClient需要配置SSLContext和TrustStrategy,处理证书和信任问题。 10. **最佳实践**: - 关闭响应实体和HttpClient实例以释放资源。 - 避免长时间持有HttpClient实例...
当我们使用HttpClient进行HTTPS请求时,有时会遇到证书相关的问题,如服务器证书不受信任或未被系统信任的CA(证书权威机构)签发。本篇将详细介绍如何配置HttpClient以自动接受所有https的证书,以便在进行测试或...
在使用HttpClient发送POST请求时,我们需要关心两个方面的问题:一是如何忽略证书验证过程,二是如何发送POST请求。对于前者,我们可以使用X509TrustManager来忽略证书验证过程。对于后者,我们可以使用HttpPost对象...
HttpClient代理访问网络