请告诉我禁用证书验证使用Unirest作为rest客户端的方法。
我在Java中使用Unirest。下面是我的源代码:
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());
}结果:
sun.security.provider.certpath.SunCertPathBuilderException:客户端错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX路径构建失败:==================REST无法找到被请求目标的有效证书路径
发布于 2020-09-21 12:15:01
我知道这条线很旧,但只是为了防止其他人在寻找更简单的方法。
Unirest现在已经内置了对此的支持。
Unirest.config().verifySsl(false);
这对我有用。
发布于 2018-12-19 09:00:27
在触发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());
}顺便说一下,请注意asJson方法返回的是JsonNode类型,而不是字符串。
发布于 2021-09-03 17:27:25
使用此设置验证ssl false
Unirest.config().verifySsl(false)注意:如果使用的是com.mashape.unirest库v1.4.9,那么在其中找不到config()方法。
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>我切换到kong.unitrestjava,它有本机config()方法来设置verifySsl false。
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.11.13</version>https://stackoverflow.com/questions/48580270
复制相似问题