java - Unirest Java客户端: kong. unirest.UnirestException : org. apache.http.client.ClientProtocolException

标签 java spring rest-client unirest unirest-java

我正在使用 Unirest Java 客户端,如下所示连接到外部 API:

 public static String loginAsAdmin(String authenticationURL , String userName , String password){
        Map<String,String> creds =  new HashMap<>();
        creds.put("username", userName);
        creds.put("password",password);
        HttpResponse<JsonNode> jsonResponse
                = Unirest.post(authenticationURL).header("Content-Type","application/json")
                .body(creds)
                .asJson();

        return jsonResponse.getBody().toString();
    }

但是当我运行代码时,我收到以下错误:

kong.unirest.UnirestException:org.apache.http.client.ClientProtocolException

堆栈跟踪:

kong.unirest.UnirestException: org.apache.http.client.ClientProtocolException

    at kong.unirest.DefaultInterceptor.onFail(DefaultInterceptor.java:43)
    at kong.unirest.apache.ApacheClient.request(ApacheClient.java:133)
    at kong.unirest.BaseRequest.asJson(BaseRequest.java:232)

有关信息(我正在使用以下 Unirest 依赖项):

 <dependency>
            <groupId>com.konghq</groupId>
            <artifactId>unirest-java</artifactId>
            <version>3.6.00</version>
        </dependency>

最佳答案

奇怪的是,这个错误消息实际上具有误导性。此问题的原因是端点 URL 错误,从而导致 404。我用正确的 URL 替换,它工作正常。也许 unirest 错误消息需要正确处理它以表示正确的错误。

关于java - Unirest Java客户端: kong. unirest.UnirestException : org. apache.http.client.ClientProtocolException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60407137/

相关文章:

java - 为什么 jsonParser.getCodec().readTree(jsonParser).asText() 为我返回一个空字符串?

java - 从 application.properties Spring Boot 中读取值

spring-mvc - 如何使用 Spring 的 REST 模板传递自定义对象

java - Elasticsearch Rest 客户端仍然给出 IOException : Too Many Open Files

java - 为什么 IDEA 忽略我的 testng 测试?

java - CRest日志请求/响应体

java - 垃圾收集率与对象分配率

java - java 中 EXCEL 的平台独立连接字符串

java - log4j 附加程序仅将我的消息发送到 stdout,但不发送远程 GELF 附加程序

Java 8 从输入文件流式创建多个对象