繁体   English   中英

如何解析字符串 Unirest 响应

[英]How to parse String Unirest Response

我有一个接收字符串响应的 unirest 请求,但在解析其结果时遇到问题。 我做了一项研究,但我看到的只是关于如何解析 json 请求的教程,这显然不是我需要的。 无论如何,请求看起来像这样:

public static String getToken() {
    //HttpResponse<String> response = null;
    try {
        //Unirest request
        HttpResponse<String> response = Unirest.post("http://api.nuvelco.com/token")
                .header("content-type", "application/x-www-form-urlencoded")
                .header("cache-control", "no-cache")
                .body("grant_type=password&username=demo_user&password=demo_pass&client_id=paymentApp")
                .asString();

        //attempts I took to print a proper response on the logs
        logger.info("RAW BODY: " + response.getRawBody());          
        logger.info("BODY: " + response.getBody());
        logger.info("STATUS: " + response.getStatus());

        return null;
    } catch (UnirestException e) {
        e.printStackTrace();
        return null;
    }
}

响应应该如下所示:

{
 "access_token": "sample_access_token",
 "token_type": "bearer",
 "expires_in": 3599,
 "refresh_token": "sample_refresher_token",
 "as:client_id": "paymentApp",
 "username": "demo_user",
 ".issued": "Thu, 08 Jun 2017 06:19:50 GMT",
 ".expires": "Thu, 08 Jun 2017 07:19:50 GMT"
}

但不知何故,我总是得到这样的回应:

<html>
    <head><title>404 Not Found</title></head>
    <body bgcolor="white">
        <center><h1>404 Not Found</h1></center>
        <hr><center>nginx/1.13.4</center>
    </body>
</html>

我不确定它是否相关,但我使用 javascript 来调用 Java 函数getToken() 任何帮助将非常感激。 谢谢你。

是否不添加Accept: application/json来请求标头帮助?

尝试在Unirest.post("http://api.nuvelco.com/token")添加/到 url。 有些像这样:

Unirest.post("http://api.nuvelco.com/token/")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM