当前位置:网站首页>如何绕过SSL验证
如何绕过SSL验证
2022-06-25 22:12:00 【大梦_几千秋】
如何绕过SSL验证
在开发中有时间会遇到访问原网站会有提示有风险,需要点击高级继续访问,这个时间使用HttpClient模仿发送请求就需要绕过SSL验证,下面是如何绕过SSL验证
- 首先创建一个工具类,将这个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();
}
}
- 接下来就可以去正常的写代码
//使用工具类中的方法创建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");
- 这样就可以绕过SSL验证,获取与原网站相同的响应,再去解析
边栏推荐
- js实现输入开始时间和结束时间,输出其中包含多少个季,并且把对应年月打印出来
- Some common operation methods of array
- 文献调研(三):数据驱动的建筑能耗预测模型综述
- Realize the conversion between analog quantity value and engineering quantity value in STEP7_ Old bear passing by_ Sina blog
- IDEA常用快捷键
- 10.3.1、FineBI_ Installation of finebi
- 《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)
- static关键字详解
- Several common rich text editors
- 文献调研(四):基于case-based reasoning、ANN、PCA的建筑小时用电量预测
猜你喜欢

在win10下使用visual studio2015链接mysql数据库

数组常用的一些操作方法

How postman tests interfaces that require login

文献调研(三):数据驱动的建筑能耗预测模型综述

Literature research (I): hourly energy consumption prediction of office buildings based on integrated learning and energy consumption pattern classification

Keil compilation run error, missing error: # 5: # includecore_ cm3.h_ Old bear passing by_ Sina blog

11.1.1、flink概述_flink概述

详细讲解局部变量、全局变量、静态变量三种类型

文献调研(一):基于集成学习和能耗模式分类的办公楼小时能耗预测
![Find the minimum value of flipped array [Abstract bisection]](/img/b9/1e0c6196e6dc51ae2c48f6c5e83289.png)
Find the minimum value of flipped array [Abstract bisection]
随机推荐
Unsigned and signed vernacular
关于scrapy爬虫时,由spider文件将item传递到管道的方法注意事项
Redis之跳跃表
Static keyword explanation
Record a simple question with ideas at the moment of brushing leetcode - Sword finger offer 09 Implementing queues with two stacks
如何配置SQL Server 2008管理器_过路老熊_新浪博客
剑指 Offer 48. 最长不含重复字符的子字符串
兆欧表电压档位选择_过路老熊_新浪博客
Summary of c++ references and pointers
网络协议之:redis protocol详解
On the use of bisection and double pointer
Installation of third-party library iGraph for social network visualization
14.1.1 promethues monitoring, four data types metrics, pushgateway
西门子S7-200PLC和丹佛斯变频器的通讯协议改造_过路老熊_新浪博客
JS中的原型链面试题--Foo和 getName
Simulation connection between WinCC and STEP7_ Old bear passing by_ Sina blog
10.2.3、Kylin_ The dimension is required for kylin
Studio5k v28安装及破解_过路老熊_新浪博客
手工制作 pl-2303hx 的USB轉TTL電平串口的電路_過路老熊_新浪博客
社交网络可视化第三方库igraph的安装