HttpClient4.3 创建SSL协议的HttpClient对象

阅读更多
public class HttpClientUtils
{
	public static CloseableHttpClient createSSLClientDefault(CookieStore cookies){
		SSLContext sslContext=null;
		
		try
		{
			sslContext=new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy(){

				@Override
				public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException
				{
					
//信任所有
					return true;
				}
				
			}).build();
			
		}
		catch (KeyManagementException e)
		{
			e.printStackTrace();
		}
		catch (NoSuchAlgorithmException e)
		{
			e.printStackTrace();
		}
		catch (KeyStoreException e)
		{
			e.printStackTrace();
		}
		 
		SSLConnectionSocketFactory sslsf=new SSLConnectionSocketFactory(sslContext);
		return HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCookieStore(cookies).build();
		
	}
}

你可能感兴趣的:(httpclient,爬虫,ssl)