Unirest 是一套跨语言轻量级HTTP开发库由Kong团队维护,此团队同时维护着另一个著名开源网关项目API Gateway Kong.
Unirest 支持多种语言,如Node、Ruby、Java、PHP、Python、Objective-C、.NET 等,可发起 GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS 请求
作为基于apache httpcomponent的unirest-java,提供了更为便捷、功能强大的api用于http请求,非常建议大家掌握
<!-- Pull in as a traditional dependency -->
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.11.11</version>
</dependency>
<!-- OR as a snazzy new standalone jar with shaded dependencies -->
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.11.11</version>
<classifier>standalone</classifier>
</dependency>
网上的使用示例较多,请参考如下
UniRest 使用手册
unirest-java的使用
以第三方系统集成海豚调度系统为例,演示http请求工具类的使用
以链式进行调用,使用起来非常便捷。
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));
}
}