https请求,Java代码忽略https证书:解决No subject alternative names present问题
https请求,Java代码忽略https证书:解决No subject alternative names present问题
package com.test.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.net.ssl.*; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; /** * Java代码忽略https证书:解决No subject alternative names present问题 */ public class HttpsUtils { private static final Logger logger = LoggerFactory.getLogger(HttpsUtils.class); /** * * @param hosturl * @param params * @return * @throws IOException * @throws KeyManagementException * @throws NoSuchAlgorithmException */ public static String post(String hosturl,String params) throws IOException, KeyManagementException, NoSuchAlgorithmException { HttpsURLConnection.setDefaultHostnameVerifier(new HttpsUtils().new NullHostNameVerifier()); SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); URL url = new URL(hosturl+"?"+params); logger.info("参数:"+hosturl+"?"+params); // 打开restful链接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST");// POST GET PUT DELETE // 设置访问提交模式,表单提交 conn.setRequestProperty("Content-Type", "text/plain;charset=utf-8"); conn.setConnectTimeout(130000);// 连接超时 单位毫秒 conn.setReadTimeout(130000);// 读取超时 单位毫秒 // 读取请求返回值 byte bytes[] = new byte[1024]; InputStream inStream = conn.getInputStream(); inStream.read(bytes, 0, inStream.available()); logger.info("返回结果:" + new String(bytes, "utf-8")); return new String(bytes, "utf-8"); } static TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { // TODO Auto-generated method stub } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { // TODO Auto-generated method stub } @Override public X509Certificate[] getAcceptedIssuers() { // TODO Auto-generated method stub return null; } } }; public class NullHostNameVerifier implements HostnameVerifier { /* * (non-Javadoc) * * @see javax.net.ssl.HostnameVerifier#verify(java.lang.String, * javax.net.ssl.SSLSession) */ @Override public boolean verify(String arg0, SSLSession arg1) { // TODO Auto-generated method stub return true; } } }
分类:
http&https请求方式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY