httpClient4.0实现https访问是避免ssl证书认证

本文通过示例代码展示了如何使用 HttpClient4.0 在进行 HTTPS 访问时跳过 SSL 证书验证,以实现不受限制的网络请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

          直接上代码啦!!!

/**
 * 绕过验证 (不足:同时会影响整个tomcat里面对证书的验证)
 * 
 * MyEclipse Struts Creation date: 2018-8-9
 * 
 * @author
 * 
 */
public class SSL {
	public static void disableChecks() {
		/*try {
			new URL("https://0.0.0.0/").getContent();
		} catch (IOException e) {
			// This invocation will always fail, but it will register the
			// default SSL provider to the URL class.
		}*/
		try {
			SSLContext sslc;
			sslc = SSLContext.getInstance("TLS");
			TrustManager[] trustManagerArray = { new X509TrustManager() {
				public void checkClientTrusted(X509Certificate[] chain,
						String authType) throws CertificateException {
					// 检查客户端证书信任
				}

				public void checkServerTrusted(X509Certificate[] chain,
						String authType) throws CertificateException {
					// 检查服务端证书验证
				}

				public X509Certificate[] getAcceptedIssuers() {
					return new X509Certificate[0];
				}
			} };
			//初始化SSLContext实例  
			sslc.init(null, trustManagerArray, null);
			HttpsURLConnection.setDefaultSSLSocketFactory(sslc
					.getSocketFactory());
			//取消对证书的所有验证
			HttpsURLConnection
					.setDefaultHostnameVerifier(new HostnameVerifier() {
						public boolean verify(String arg0, SSLSession arg1) {
							return false;
						}
					});
		} catch (Exception e) {
			throw new IllegalArgumentException("证书校验异常!");
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值