Unirest工具类的使用


官方文档: http://kong.github.io/unirest-java/
引入依赖:

<!-- Pull in as a traditional dependency -->
<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>3.14.1</version>
</dependency>

<!-- OR as a snazzy new standalone jar with shaded dependencies -->
<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>3.14.1</version>
    <classifier>standalone</classifier>
</dependency>

用法示例:

import kong.unirest.Unirest;

class UniRestTest {
    public static void main(String[] args) throws Exception {
        String res = "";

        res = Unirest.post("http://172.25.xx.xx:12345/dolphinscheduler/login")
                .header("accept", "application/json")
                .field("userName", "admin")
                .field("userPassword", "dolphinscheduler123")
                .asString()
                .getBody();
        printMsg("login res:%s", res);

        res = Unirest.get("http://172.25.xx.xx:12345/dolphinscheduler/log/detail")
                .header("accept", "application/json")
                .header("token", "ca3e91749eee187fa9a797d92cf5cb6d")
                .queryString("taskInstanceId", "703")
                .queryString("skipLineNum", "0")
                .queryString("limit", "1000")
                .asString()
                .getBody();
        printMsg("log detail res:%s", res);
    }

    private static void printMsg(String template, Object... args) {
        System.out.println(String.format(template, args));
    }
}

文章作者: zhou22
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 zhou22 !
  目录