当前位置: 首页 > news >正文

Https post 请求时绕过证书验证方案

解决异常:
Caused by: java.security.cert.CertificateException: No subject alternative names matching IP address xxx.xx.xx.xx found

// Https POST 请求private cn.hutool.json.JSON PostGsData(String url, String appKey, String token, Map<String, Object> params) {try {// 发送请求参数String data = JSONObject.toJSONString(params);URL realUrl = new URL(url);//绕过证书验证,验证主机名和服务器验证方案的匹配是可接受的TrustManager[] trustAllCerts = new TrustManager[1];trustAllCerts[0] = new TrustAllManager();SSLContext sc = SSLContext.getInstance("SSL");sc.init(null, trustAllCerts, null);HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());HttpsURLConnection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());//获取HttpsURLConnection链接对象HttpsURLConnection connection = (HttpsURLConnection) realUrl.openConnection();connection.setRequestMethod("POST");connection.setRequestProperty("AppKey", appKey);connection.setRequestProperty("Content-Type", "application/json");// 设置通用的请求属性
//            connection.setRequestProperty("accept", "*/*");
//            connection.setRequestProperty("connection", "Keep-Alive");
//            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//            connection.setRequestProperty("content-length", contentJson.getBytes().length + "");if (StringUtil.isNotEmpty(token)) {connection.setRequestProperty("Authorization", token);}// 发送POST请求必须设置如下两行connection.setDoOutput(true);connection.setDoInput(true);// 获取URLConnection对象对应的输出流PrintWriter out = new PrintWriter(connection.getOutputStream());out.print(data);// flush输出流的缓冲out.flush();// 定义BufferedReader输入流来读取URL的响应BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));String line;String result = "";while ((line = in.readLine()) != null) {result += line;}logger.info("请求返回的结果:" + result);// 关闭连接connection.disconnect();return JSONUtil.parse(result);} catch (Exception ex) {ex.printStackTrace();logger.error("请求出现异常:" + ex.getMessage());Map<String, String> res = new HashMap<>();res.put("ERRCODE", "0");res.put("ERRMSG", "请求出现异常:" + ex.getMessage());return JSONUtil.parse(res);}}

http://www.lryc.cn/news/407971.html

相关文章:

  • C# 数组常用遍历方式
  • 【JavaScript】详解Day.js:轻量级日期处理库的全面指南
  • AI算法与图像处理 | 吴恩达团队新作!多模态方向
  • 云服务器Ubuntu18.04进行Nginx配置
  • SQL labs-SQL注入(四,sqlmap对于post传参方式的注入)
  • R包:plot1cell单细胞可视化包
  • Tent混沌人工蜂群与粒子群混合算法遇到问题,具体问题及解决方案如文。
  • Python文献调研(一)环境搭建
  • URL重写
  • git配置环境变量
  • vue3编程-import.meta.glob实现动态路由(菜单)
  • 富唯智能转运机器人:高效、智能、未来的选择
  • 跨境电商独立站:Shopify/Wordpress/店匠选哪个?
  • 减轻幻觉新SOTA,7B模型自迭代训练效果超越GPT-4,上海AI lab发布
  • 53.最大子数组和,动态规划+贪心解法!!!
  • python+vue3+onlyoffice在线文档系统实战20240723笔记,项目界面设计和初步开发
  • 谷粒商城实战笔记-72-商品服务-API-属性分组-获取分类属性分组
  • Vue 自定义指令
  • 【python】python图书管理系统_普通用户+管理员菜单(源码+论文)【独一无二】
  • 智能路面裂缝检测:基于YOLO和深度学习的全流程实现
  • C++ unordered_map
  • PHP Switch 语句
  • electron 网页TodoList应用打包win桌面软件数据持久化
  • 软件缺陷(Bug)、禅道
  • MySQL客户端命令一节将.sql文件导入MySQL
  • [论文笔记] DCA(Dual Chunk Attention)
  • 构建查询洞察 UI
  • 【北京迅为】《i.MX8MM嵌入式Linux开发指南》-第三篇 嵌入式Linux驱动开发篇-第五十九章 等待队列
  • 35.【C语言】详解函数递归
  • 【机器学习】智驭未来:机器学习如何重塑制造业的转型与升级