1.发送请求 import cn.hutool.Http.HttpRequest;public class TqOdpServiceClient { private static String url="url";; pub
import cn.hutool.Http.HttpRequest;public class TqOdpServiceClient { private static String url="url";; public static String execute(String http,String params,String auth) { String result2 = HttpRequest.post(http+url) .header("Authorization", auth) .header("Content-Type", "application/JSON;charset=UTF-8").setSSLSocketFactory(SSLUtils.getSSLSocketFactory()) .body(params) .execute().body(); return result2; }}
import javax.net.ssl.*;import java.security.SecureRandom;import java.security.cert.X509Certificate;public class SSLUtils { 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); } } private static TrustManager[] getTrustManager() { TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { //检查客户端证书,若不信任该证书抛出异常,咱们自己就是客户端不用检查 @Override public void checkClientTrusted(X509Certificate[] chain, String authType) { } //检查服务器的证书,若不信任该证书抛出异常,可以不检查默认都信任 @Override public void checkServerTrusted(X509Certificate[] chain, String authType) { } //返回受信任的X509证书数组 @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; } } }; return trustAllCerts; }}
来源地址:https://blog.csdn.net/daohangtaiqian/article/details/131551228
--结束END--
本文标题: 【java】hutool发送http请求,配置ssl忽略
本文链接: https://www.lsjlt.com/news/375270.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0