首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java Unirest禁用cerificate

Java Unirest禁用cerificate
EN

Stack Overflow用户
提问于 2018-02-02 10:05:55
回答 3查看 3.9K关注 0票数 3

请告诉我禁用证书验证使用Unirest作为rest客户端的方法。

我在Java中使用Unirest。下面是我的源代码:

代码语言:javascript
运行
复制
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无法找到被请求目标的有效证书路径

EN

回答 3

Stack Overflow用户

发布于 2020-09-21 12:15:01

我知道这条线很旧,但只是为了防止其他人在寻找更简单的方法。

Unirest现在已经内置了对此的支持。

Unirest.config().verifySsl(false);

这对我有用。

票数 5
EN

Stack Overflow用户

发布于 2018-12-19 09:00:27

在触发http post请求之前,请将自定义http客户端设置为Unirest,如下所示:

代码语言:javascript
运行
复制
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类型,而不是字符串。

票数 3
EN

Stack Overflow用户

发布于 2021-09-03 17:27:25

使用此设置验证ssl false

代码语言:javascript
运行
复制
Unirest.config().verifySsl(false)

注意:如果使用的是com.mashape.unirest库v1.4.9,那么在其中找不到config()方法。

代码语言:javascript
运行
复制
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>

我切换到kong.unitrestjava,它有本机config()方法来设置verifySsl false。

代码语言:javascript
运行
复制
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.11.13</version>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48580270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档