如何绕过SSL验证

如何绕过SSL验证

在开发中有时间会遇到访问原网站会有提示有风险,需要点击高级继续访问,这个时间使用HttpClient模仿发送请求就需要绕过SSL验证,下面是如何绕过SSL验证

  1. 首先创建一个工具类,将这个createSSLClientDefault()方法复制过去,导入所需要的的jar包
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;

public class Myutils {
	//创建HttpClients 绕过ssl验证所需工具类
	public static CloseableHttpClient createSSLClientDefault() {
		try {
			SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
				// 信任所有
				public boolean isTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
						throws java.security.cert.CertificateException {
					
					return true;
				}
			}).build();
			HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
			return HttpClients.custom().setSSLSocketFactory(sslsf).build();
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyStoreException e) {
			e.printStackTrace();
		}
		return HttpClients.createDefault();
	}

}
  1. 接下来就可以去正常的写代码
//使用工具类中的方法创建httpClient实例
	HttpClient client = SSL.createSSLClientDefault();

//发送HttpGet请求 模板
	//创建HttpClient 
	HttpClient  httpclient = createSSLClientDefault();
	//发送接口地址
	HttpGet httpget = new HttpGet("https://127.0.0.1/postandget/testget?iw-apikey=123&iw-cmd=testget¶mValue=1111");
	//发送请求并接收response 
	HttpResponse httpresponse = httpclient.execute(httpget);
	String result = EntityUtils.toString(httpresponse.getEntity(), "UTF-8");

//发送httpPost请求 模板
	//创建HttpClient 
		HttpClient  httpclient = createSSLClientDefault();
		//发送接口地址
		HttpPost httppost = new HttpPost("https://127.0.0.1/postandget/testpost");
		//设置请求体格式Content-Type
		httppost.setHeader("Content-Type", "application/json");
		//定义String请求Json参数体
		httppost.setEntity(new StringEntity(new String("{\"iw-apikey\":\"123\", \"paramValue1\":\"123\",\"paramValue2\":\"1234\"}"), Charset.forName("UTF-8")));
		//发送请求并接收response 
		HttpResponse httpresponse = httpclient.execute(httppost);
		String result = EntityUtils.toString(httpresponse.getEntity(), "UTF-8");
  1. 这样就可以绕过SSL验证,获取与原网站相同的响应,再去解析

你可能感兴趣的:(HttpClient,java)