Java Unirest禁用证书
请告诉我如何禁用证书验证以将Unirest用作rest客户端 我使用Unirest和JavaSpring。下面是我的源代码:Java Unirest禁用证书,java,spring,spring-boot,unirest,Java,Spring,Spring Boot,Unirest,请告诉我如何禁用证书验证以将Unirest用作rest客户端 我使用Unirest和JavaSpring。下面是我的源代码: try { HttpResponse<String> response = Unirest.post("myurl") .header("content-type", "application/json") .header("accept", "application/json") .body("my json da
try {
HttpResponse<String> response = Unirest.post("myurl")
.header("content-type", "application/json")
.header("accept", "application/json")
.body("my json data here")
.asJson();
} catch (Exception e) {
logger.info("==================REST CLIENT ERROR: "+e.getMessage());
}
试试看{
HttpResponse response=Unirest.post(“myurl”)
.header(“内容类型”、“应用程序/json”)
.header(“接受”、“应用程序/json”)
.body(“此处为我的json数据”)
.asJson();
}捕获(例外e){
logger.info(“=========================REST客户端错误:“+e.getMessage());
}
结果是:
=====================REST客户端错误:javax.net.ssl.SSLHandshakeException:
sun.security.validator.validator异常:PKIX路径生成失败:
sun.security.provider.certpath.SunCertPathBuilderException:无法
找到请求目标的有效证书路径
在触发http post请求之前,请将自定义http客户端设置为Unirest,如下所示:
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) {
return true;
}
}).build();
HttpClient customHttpClient = HttpClients.custom().setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
Unirest.setHttpClient(customHttpClient);
HttpResponse<JsonNode> response = Unirest.post("myurl")
.header("content-type", "application/json")
.header("accept", "application/json")
.body("my json data here")
.asJson();
} catch (Exception e) {
logger.info("==================REST CLIENT ERROR: " + e.getMessage());
}
试试看{
SSLContext SSLContext=新的SSLContextBuilder()。加载信任材料(空,新的TrustSelfSignedStrategy()){
已信任公共布尔值(X509Certificate[]链,字符串authType){
返回true;
}
}).build();
HttpClient customHttpClient=HttpClients.custom().setSSLContext(sslContext)
.setSSLHostnameVerifier(新的NoopHostnameVerifier()).build();
Unirest.setHttpClient(customHttpClient);
HttpResponse response=Unirest.post(“myurl”)
.header(“内容类型”、“应用程序/json”)
.header(“接受”、“应用程序/json”)
.body(“此处为我的json数据”)
.asJson();
}捕获(例外e){
logger.info(“=========================REST客户端错误:“+e.getMessage());
}
顺便说一句,请注意,asJson方法返回的是JsonNode类型,而不是字符串。我知道这个线程非常旧,但只是为了防止其他人寻找更简单的方法而编写 Unirest现在已经内置了对此的支持
Unirest.config().verifySsl(false)代码>
这对我很有用。谢谢你的快速调整。