• CloseableHttpClient 和 OkHttpClient跳过证书验证


    一:CloseableHttpClient 

    public static String doPostJson(String url, String json) {
    	// 创建Httpclient对象
    	CloseableHttpClient httpClient = HttpClients.createDefault();
    	CloseableHttpResponse response = null;
    	String resultString = "";
    	try {
    		if (url.indexOf("https") != -1) {
    			httpClient = HttpClients.custom()
    					.setSSLSocketFactory(new SSLConnectionSocketFactory(
    							SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()))
    					.build();
    		}
    		// 创建Http Post请求
    		HttpPost httpPost = new HttpPost(url);
    		// 创建请求内容
    		StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
    		httpPost.setEntity(entity);
    		// 执行http请求
    		response = httpClient.execute(httpPost);
    		resultString = EntityUtils.toString(response.getEntity(), "utf-8");
    	} catch (Exception e) {
    		e.printStackTrace();
    	} finally {
    		try {
    			response.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	return resultString;
    }
    

    二:OkHttpClient 

    OkHttpClient okHttpClient = new OkHttpClient.Builder().readTimeout(60, TimeUnit.SECONDS)
    				.connectTimeout(60, TimeUnit.SECONDS)
    				.sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getX509TrustManager())
    				.hostnameVerifier(SSLSocketClient.getHostnameVerifier()).build();
    
    import javax.net.ssl.*;
    import java.security.KeyStore;
    import java.security.SecureRandom;
    import java.security.cert.X509Certificate;
    import java.util.Arrays;
    
    public class SSLSocketClient {
    
    	/**
    	 * 获取这个SSLSocketFactory
    	 */
    	public static SSLSocketFactory getSSLSocketFactory() {
    		try {
    			SSLContext sslContext = SSLContext.getInstance("SSL");
    			sslContext.init(null, getTrustManager(), new SecureRandom());
    			return sslContext.getSocketFactory();
    		} catch (Exception e) {
    			throw new RuntimeException(e);
    		}
    	}
    
    	/**
    	 * 获取TrustManager
    	 */
    	private static TrustManager[] getTrustManager() {
    		return new TrustManager[] { new X509TrustManager() {
    			@Override
    			public void checkClientTrusted(X509Certificate[] chain, String authType) {
    			}
    
    			@Override
    			public void checkServerTrusted(X509Certificate[] chain, String authType) {
    			}
    
    			@Override
    			public X509Certificate[] getAcceptedIssuers() {
    				return new X509Certificate[] {};
    			}
    		} };
    	}
    
    	/**
    	 * 获取HostnameVerifier
    	 */
    	public static HostnameVerifier getHostnameVerifier() {
    		return (s, sslSession) -> true;
    	}
    
    	public static X509TrustManager getX509TrustManager() {
    		X509TrustManager trustManager = null;
    		try {
    			TrustManagerFactory trustManagerFactory = TrustManagerFactory
    					.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    			trustManagerFactory.init((KeyStore) null);
    			TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
    			if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
    				throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
    			}
    			trustManager = (X509TrustManager) trustManagers[0];
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    
    		return trustManager;
    	}
    
    }
    

      

     参考文章:https://www.cnblogs.com/todarcy/p/15104125.html

                       

      

      

  • 相关阅读:
    golang 简单的实现内 网 穿 透,用户访问本地服务。
    golang 创建一个简单的广播echo服务器
    golang 使用 protobuf 的教程
    golang语言中os包的学习与使用(文件,目录,进程的操作)
    【原】画流程图工具visio使用技巧汇总
    【改】IOS-百度地图API用点生成线路、导航、自定义标注 2013年11月更新
    【原】xcode5&IOS7及以下版本免证书真机调试记录
    【转】C++的拷贝构造函数深度解读,值得一看
    【转】c++中引用的全方位解读
    【转】self.myOutlet=nil、viewDidUnload、dealloc的本质剖析
  • 原文地址:https://www.cnblogs.com/haoliyou/p/15157329.html
Copyright © 2020-2023  润新知