HttpClient 如何忽略证书验证访问https - ALLOW_ALL_HOSTNAME_VERIFIER (二)

HttpClient 如何忽略证书验证访问https - ALLOW_ALL_HOSTNAME_VERIFIER(二)





/** 
	* @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.忽略证书验证


		DefaultHttpClient httpclient = (DefaultHttpClient) getNewHttpClient();
		HttpGet httpGet = new HttpGet(httpGetUrl);
		
		CookieStore cookieStore = new BasicCookieStore();
		
		BasicClientCookie cookie = new BasicClientCookie("domainName", ".hub.com");
		cookie.setVersion(0);
		cookie.setDomain(".hub.com");
		cookie.setPath("/");
		cookieStore.addCookie(cookie);
		
		// Create local HTTP context
		HttpContext localContext = new BasicHttpContext();
		// Bind custom cookie store to the local context
		localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
		
		/add cookie - END
		try {
			//HttpResponse response = httpclient.execute(httpGet);
			HttpResponse response = httpclient.execute(httpGet, localContext);
		}catch (Exception e) {}



附上not-yet-commons-ssl-0.3.11.jar

 

这个错误通常是由于SSL/TLS握手过程出现问题导致的。以下是一些可能导致此问题的原因和解决方法: 1. 证书问题:服务器使用了自签名证书或过期的证书。此时需要在代码中设置忽略证书验证或者将证书添加到本地信任列表中。 2. 协议版本问题:客户端和服务器之间的TLS协议版本不匹配。此时需要检查代码中指定的TLS版本是否与服务器支持的版本一致。 3. 代理问题:如果使用代理服务器,可能会导致SSL/TLS握手失败。此时需要检查代理服务器的设置是否正确,并确保代理服务器支持HTTPS连接。 4. 网络问题:SSL/TLS握手需要建立安全连接,如果网络连接不稳定或者中断,可能会导致握手失败。此时需要检查网络连接是否正常,并尝试重新连接。 你可以在代码中添加以下语句来忽略证书验证,检查一下是否有帮助: ```java SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); ``` 如果以上方法不能解决问题,你可以尝试使用Wireshark等工具对网络流量进行抓包分析,查看握手过程中是否有异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值