http框架--unirest介绍及使用示例

1. Unirest
1.1. 介绍
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请求,非常建议大家掌握

github地址
使用文档1.2. maven依赖

<!-- 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>

1.3. 使用手册

网上的使用示例较多,请参考如下
UniRest 使用手册
unirest-java的使用

2. 使用示例

以第三方系统集成海豚调度系统为例,演示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));
    }
}


————————————————
版权声明:本文为CSDN博主「enjoy编程」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/penriver/article/details/118360994

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值