HttpClient 如何忽略证书验证 - ALLOW_ALL_HOSTNAME_VERIFIER
1。设置可以访问HTTPS
Function - getNewHttpClient
/**
* @Title: getNewHttpClient
* @Description: Methods Description
* @param @return
* @return HttpClient
* @throws
*/
private HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore
.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new SSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(
params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
2.忽略证书验证
Class: SSLSocketFactory
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
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.conn.ssl.SSLSocketFactory;
public class SSLSocketFactory extends SSLSocketFactory {
/**
* @Fields sslContext
* @Description: Field Description
*/
SSLContext sslContext = SSLContext.getInstance("TLS");
/**
* <p>Title: </p>
* <p>Description: </p>
* @param truststore
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws KeyStoreException
* @throws UnrecoverableKeyException
*/
public SSLSocketFactory(KeyStore truststore)
throws NoSuchAlgorithmException, KeyManagementException,
KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] { tm }, null);
}
/* (non-Javadoc)
* <p>Title: createSocket</p>
* <p>Description: </p>
* @param socket
* @param host
* @param port
* @param autoClose
* @return
* @throws IOException
* @throws UnknownHostException
* @see org.apache.http.conn.ssl.SSLSocketFactory#createSocket(java.net.Socket, java.lang.String, int, boolean)
*/
@Override
public Socket createSocket(Socket socket, String host, int port,
boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port,
autoClose);
}
/* (non-Javadoc)
* <p>Title: createSocket</p>
* <p>Description: </p>
* @return
* @throws IOException
* @see org.apache.http.conn.ssl.SSLSocketFactory#createSocket()
*/
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
3。调用并用HTTPS访问
DefaultHttpClient httpclient = (DefaultHttpClient) getNewHttpClient();
try {
//Secure Protocol implementation.
SSLContext ctx = SSLContext.getInstance("SSL");
//Implementation of a trust manager for X509 certificates
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ClientConnectionManager ccm = httpclient.getConnectionManager();
//register https protocol in httpclient's scheme registry
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
} catch (Exception e) {
e.printStackTrace();
}
HttpGet httpGet = new HttpGet(httpGetUrl);
HttpResponse response = httpclient.execute(httpGet, localContext);
分享到:
相关推荐
使用HttpClient4.5实现https请求忽略SSL证书验证工具类
这个实例主要涉及如何配置HttpClient来忽略SSL(Secure Socket Layer)验证,这对于在开发和测试环境中处理自签名证书或未认证的服务器非常有用。以下将详细介绍HttpClient的使用以及如何进行SSL验证的忽略。 首先...
- **HTTP客户端API**:提供了一个新的标准HTTP客户端API(java.net.http.HttpClient),以替代旧的sun.net.www.http.HttpClient。 8. **JDK的安装与配置**:安装"jdk-10_windows-x64_bin.exe"后,需要设置环境变量...
请注意,这只是一个基本示例,实际应用中应考虑更多的安全因素,例如正确处理证书链、支持不同的加密算法,以及配置证书验证策略。 在"Client https server.doc"文档中,可能会包含更详细的步骤、示例代码和注意...
赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...
赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...
赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...
赠送jar包:httpclient-4.5.12.jar; 赠送原API文档:httpclient-4.5.12-javadoc.jar; 赠送源代码:httpclient-4.5.12-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.12.pom; 包含翻译后的API文档:...
- 轻量级HTTP客户端API(java.net.http.HttpClient):提供了用于执行HTTP和HTTPS请求的新API,简化了网络编程。 - 密封类(Sealed Classes):增强了类型安全,限制了哪些其他类可以扩展或实现特定的类或接口。 - ...
STM3210X HTTP_Client
赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...
根据提供的文件信息,我们可以深入探讨如何使用`httpclient`库来进行`https`访问,并了解其中涉及的关键概念和技术细节。 ### 标题与描述解析:使用`httpclient`进行`https`访问 #### 1. `httpclient`简介 `...
最后,`commons-httpclient-3.1.jar`作为主要的HTTP客户端库,负责建立连接、发送请求和接收响应。 在开发过程中,将这些库包含到项目的类路径中是必要的,这样就可以利用它们的功能来执行HTTP操作。然而,需要注意...
socketFactory.setHostnameVerifier(EasySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ``` ### 2. Android中的HttpsURLConnection `HttpsURLConnection`是Java标准库提供的用于HTTPS通信的API,它比`...
赠送jar包:httpclient-4.5.5.jar; 赠送原API文档:httpclient-4.5.5-javadoc.jar; 赠送源代码:httpclient-4.5.5-sources.jar; 包含翻译后的API文档:httpclient-4.5.5-javadoc-API文档-中文(简体)版.zip ...
注:下文中的 *** 代表文件名中的组件名称。 # 包含: 中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 ...
这个名为"Android-Httpclient_Get_Post"的资源可能是一个示例项目或教程,展示了如何在Android应用中使用`HttpClient`进行网络数据的获取与提交。 首先,`HttpClient`是Apache的一个组件,它提供了对HTTP协议的全面...
2013-11-21 15:31 292,890 httpclient-4.0.2.jar 2017-12-20 12:08 351,132 httpclient-4.1.1.jar 2012-08-03 01:45 451,595 httpclient-4.1.2-sources.jar 2012-08-03 01:44 352,254 httpclient-4.1.2.jar 2012-08-...
赠送jar包:httpclient-4.5.2.jar; 赠送原API文档:httpclient-4.5.2-javadoc.jar; 赠送源代码:httpclient-4.5.2-sources.jar; 包含翻译后的API文档:httpclient-4.5.2-javadoc-API文档-中文(简体)-英语-对照...
赠送jar包:httpclient-4.4.1.jar; 赠送原API文档:httpclient-4.4.1-javadoc.jar; 赠送源代码:httpclient-4.4.1-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.1.pom; 包含翻译后的API文档:httpclient...