超级好用的java http请求工具

kong-http

基于okhttp封装的轻量级http客户端


使用方式

Maven

xml 复制代码
<dependency>
    <groupId>io.github.kongweiguang</groupId>
    <artifactId>kong-http</artifactId>
    <version>0.1</version>
</dependency>

Gradle

复制代码
implementation 'io.github.kongweiguang:kong-http:0.1'

Gradle-Kotlin

复制代码
implementation("io.github.kongweiguang:kong-http:0.1")

简单介绍

请求对象

java 复制代码
public class ObjTest {

    @Test
    void test1() throws Exception {

        //自定义请求创建
        Req.of().method(Method.GET).url("http://localhost:8080/get");

        //基本的http请求
        Req.get("http://localhost:8080/get");
        Req.post("http://localhost:8080/post");
        Req.delete("http://localhost:8080/delete");
        Req.put("http://localhost:8080/put");
        Req.patch("http://localhost:8080/patch");
        Req.head("http://localhost:8080/head");
        Req.options("http://localhost:8080/options");
        Req.trace("http://localhost:8080/trace");
        Req.connect("http://localhost:8080/connect");

        //特殊http请求
        //application/x-www-form-urlencoded
        Req.formUrlencoded("http://localhost:8080/formUrlencoded");
        //multipart/form-data
        Req.multipart("http://localhost:8080/multipart");

        //ws协议请求创建
        Req.ws("http://localhost:8080/ws");

        //sse协议请求创建
        Req.sse("http://localhost:8080/sse");

    }

}

url请求地址

url添加有两种方式,可以混合使用,如果url和构建函数里面都有值,按构建函数里面为主

  • 直接使用url方法
java 复制代码
public class UrlTest {

    @Test
    void test1() throws Exception {
        final Res res = Req.get("http://localhost:8080/get/one/two").ok();
        System.out.println("res = " + res.str());
    }


    // 使用构建方法
    @Test
    void test2() {
        final Res res = Req.of()
                .scheme("http")
                .host("localhost")
                .port(8080)
                .path("get")
                .path("one")
                .path("two")
                .ok();
        System.out.println("res.str() = " + res.str());
        // http://localhost:8080/get/one/two
    }

    //混合使用
    @Test
    void test3() throws Exception {
        // http://localhost:8080/get/one/two
        final Res res = Req.get("/get")
                .scheme("http")
                .host("localhost")
                .port(8080)
                .path("one")
                .path("two")
                .ok();
        System.out.println("res = " + res.str());
    }
}

url参数

java 复制代码
public class UrlQueryTest {

    @Test
    void test1() throws Exception {
        //http://localhost:8080/get/one/two?q=1&k1=v1&k2=1&k2=2&k3=v3&k4=v4
        final Res res = Req.get("http://localhost:8080/get/one/two?q=1")
                .query("k1", "v1")
                .query("k2", Arrays.asList("1", "2"))
                .query(new HashMap<String, String>() {{
                    put("k3", "v3");
                    put("k4", "v4");
                }})
                .ok();
    }

}

请求头

设置请求头内容,cookie等

java 复制代码
public class HeaderTest {

    @Test
    void test1() throws Exception {
        final Res res = Req.get("http://localhost:8080/header")
                //contentype
                .contentType(ContentType.json)
                //charset
                .charset(StandardCharsets.UTF_8)
                //user-agent
                .ua(Mac.chrome.v())
                //authorization
                .auth("auth qwe")
                //authorization bearer
                .bearer("qqq")
                //header
                .header("name", "value")
                //headers
                .headers(new HashMap<String, String>() {{
                    put("name1", "value1");
                    put("name2", "value2");
                }})
                //cookie
                .cookie("k", "v")
                //cookies
                .cookies(new HashMap<String, String>() {{
                    put("k1", "v1");
                    put("k2", "v2");
                }})
                .ok();
        System.out.println("res.str() = " + res.str());
    }

}

请求体

get和head请求就算添加了请求体也不会携带在请求

java 复制代码
public class BodyTest {

    @Test
    void test1() throws Exception {
        final User kkk = new User().setAge(12).setHobby(new String[]{"a", "b", "c"}).setName("kkk");
        final Res res = Req.post("http://localhost:8080/post_body")
                //        .body(JSON.toJSONString(kkk))
                //        .body("{}")
                //自动会将对象转成json对象,使用jackson
                .json(kkk)
                //        .body("text", ContentType.text_plain)
                .ok();
        System.out.println("res.str() = " + res.str());
    }

}

form表单请求

可发送application/x-www-form-urlencoded表单请求,如果需要上传文件则使用multipart/form-data

java 复制代码
public class FormTest {

    @Test
    void testForm() throws IOException {
        //application/x-www-form-urlencoded
        final Res ok = Req.formUrlencoded("http://localhost:8080/post_form")
                .form("a", "1")
                .form(new HashMap<String, String>() {{
                    put("b", "2");
                }})
                .ok();
        System.out.println("ok.str() = " + ok.str());
    }

    @Test
    void test2() throws Exception {
        //multipart/form-data
        final Res ok = Req.multipart("http://localhost:8080/post_mul_form")
                .file("k", "k.txt", Files.readAllBytes(Paths.get("D:\\k\\k.txt")))
                .form("a", "1")
                .form(new HashMap<String, String>() {{
                    put("b", "2");
                }})
                .ok();
        System.out.println("ok.str() = " + ok.str());
    }
}

异步请求

异步请求返回的是future,也可以使用join()或者get()方法等待请求执行完,具体使用请看CompletableFuture(异步编排)

java 复制代码
public class AsyncTest {

    @Test
    void test1() throws Exception {
        final CompletableFuture<Res> future = Req.get("http://localhost:8080/get")
                .query("a", "1")
                .success(r -> System.out.println(r.str()))
                .fail(System.out::println)
                .okAsync();

        System.out.println("res = " + future.get(3, TimeUnit.SECONDS));
    }

}

请求超时时间设置

超时设置的时间单位是

java 复制代码
public class TimeoutTest {

    @Test
    void test1() throws Exception {
        final Res res = Req.get("http://localhost:8080/timeout")
                .timeout(3)
//        .timeout(10, 10, 10)
                .ok();
        System.out.println(res.str());
    }

}

响应对象

java 复制代码
public class ResTest {

    @Test
    void testRes() {
        final Res res = Req.get("http://localhost:80/get_string")
                .query("a", "1")
                .query("b", "2")
                .query("c", "3")
                .ok();

        //返回值
        final String str = res.str();
        final byte[] bytes = res.bytes();
        final User obj = res.obj(User.class);
        final List<User> obj1 = res.obj(new TypeRef<List<User>>() {
        }.type());
        final List<String> list = res.list();
        final Map<String, String> map = res.map();
        final JSONObject jsonObject = res.jsonObj();
        final InputStream stream = res.stream();
        final Integer i = res.rInt();
        final Boolean b = res.rBool();

        //响应头
        final String ok = res.header("ok");
        final Map<String, List<String>> headers = res.headers();

        //状态
        final int status = res.code();

        //原始响应
        final Response response = res.raw();


    }

}

重试

重试可以实现同步重试和异步重试,重试的条件可自定义实现

java 复制代码
public class RetryTest {

    @Test
    void testRetry() {
        final Res res = Req.get("http://localhost:8080/error")
                .query("a", "1")
                .retry(3)
                .ok();
        System.out.println("res = " + res.str());
    }

    @Test
    void testRetry2() {
        final Res res = Req.get("http://localhost:8080/error")
                .query("a", "1")
                .retry(3, Duration.ofSeconds(2), (r, t) -> {
                    final String str = r.str();
                    if (str.length() > 10) {
                        return true;
                    }
                    return false;
                })
                .ok();
        System.out.println("res.str() = " + res.str());
    }

    @Test
    void testRetry3() {
        //异步重试
        final CompletableFuture<Res> res = Req.get("http://localhost:8080/error")
                .query("a", "1")
                .retry(3)
                .okAsync();
        System.out.println(1);
        System.out.println("res.join().str() = " + res.join().str());
    }
}

请求代理

代理默认是http,可以设置socket代理

java 复制代码
public class ProxyTest {

    @Test
    void test1() throws Exception {

        Config.proxy("127.0.0.1", 80);
        Config.proxy(Type.SOCKS, "127.0.0.1", 80);
        Config.proxyAuthenticator("k", "pass");

        final Res res = Req.get("http://localhost:8080/get/one/two")
                .query("a", "1")
                .ok();
    }

}

下载

java 复制代码
public class DowTest {

    @Test
    void testDow() {
        final Res ok = Req.get("http://localhost:80/get_file").ok();

        try {
            ok.file("d:\\k.txt");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

添加日志

java 复制代码
public class LogTest {
    @Test
    public void test() throws Exception {
        Req.get("http://localhost:8080/get/one/two")

                .log(ReqLog.console, HttpLoggingInterceptor.Level.BODY)
                .timeout(Duration.ofMillis(1000))
                .ok()
                .then(r -> {
                    System.out.println(r.code());
                    System.out.println("ok -> " + r.isOk());
                })
                .then(r -> {
                    System.out.println("redirect -> " + r.isRedirect());
                });
    }
}

ws请求

ws请求返回的res对象为null

java 复制代码
public class WsTest {

    @Test
    void test() {

        final Res ok = Req.ws("ws://websocket/test")
                .query("k", "v")
                .wsListener(new WSListener() {
                    @Override
                    public void open(final Req req, final Res res) {
                        super.open(req, res);
                    }

                    @Override
                    public void msg(final Req req, final String text) {
                        send("hello");
                    }

                    @Override
                    public void msg(final Req req, final byte[] bytes) {
                        super.msg(req, bytes);
                    }

                    @Override
                    public void fail(final Req req, final Res res, final Throwable t) {
                        super.fail(req, res, t);
                    }

                    @Override
                    public void closing(final Req req, final int code, final String reason) {
                        super.closing(req, code, reason);
                    }

                    @Override
                    public void closed(final Req req, final int code, final String reason) {
                        super.closed(req, code, reason);
                    }
                })
                .ok();
        //res == null
        Util.sync(this);
    }

}

sse请求

sse请求返回的res对象为null

java 复制代码
public class SseTest {


    @Test
    void test() throws InterruptedException {

        Req.sse("localhost:8080/sse")
                .sseListener(new SSEListener() {
                    @Override
                    public void event(Req req, SseEvent msg) {
                        System.out.println("sse -> " + msg.id());
                        System.out.println("sse -> " + msg.type());
                        System.out.println("sse -> " + msg.data());
                        if (Objects.equals(msg.data(), "done")) {
                            close();
                        }
                    }

                    @Override
                    public void open(final Req req, final Res res) {
                        super.open(req, res);
                    }

                    @Override
                    public void fail(final Req req, final Res res, final Throwable t) {
                        super.fail(req, res, t);
                    }

                    @Override
                    public void closed(final Req req) {
                        super.closed(req);
                    }
                })
                .ok();

        Util.sync(this);
    }

}

全局配置设置

java 复制代码
public class ConfigTest {

    @Test
    void test1() throws Exception {

        //设置代理
        OK.conf()
                .proxy("127.0.0.1", 80)
                .proxy(Type.SOCKS, "127.0.0.1", 80)
                .proxyAuthenticator("k", "pass")

                //设置拦截器
                .addInterceptor(new Interceptor() {
                    @NotNull
                    @Override
                    public Response intercept(@NotNull final Chain chain) throws IOException {
                        System.out.println(1);
                        return chain.proceed(chain.request());
                    }
                })
                //设置连接池
                .connectionPool(new ConnectionPool(10, 10, TimeUnit.MINUTES))
                //设置异步调用的线程池
                .exec(Executors.newCachedThreadPool());
    }

}

单次请求配置

java 复制代码
public class SingingConfigTest {
    @Test
    public void test1() throws Exception {
        final Res res = Req.get("http://localhost:80/get_string")
                .config(c -> c.followRedirects(false).ssl(false))
                .ok();
    }
}
相关推荐
专注API从业者4 分钟前
Open Claw 京东商品监控选品实战:一键抓取、实时监控、高效选品
java·服务器·数据库
摇滚侠22 分钟前
DBeaver 导入数据库 导入 SQL 文件 MySQL 备份恢复
java·数据库·mysql
keep one's resolveY1 小时前
SpringBoot实现重试机制的四种方案
java·spring boot·后端
天空属于哈夫克31 小时前
企业微信API常见的错误和解决方案
java·数据库·企业微信
摇滚侠2 小时前
VMvare 虚拟机 Oracle19c 安装步骤,远程连接 Oracle19c,百度网盘安装包
java·oracle
梁萌2 小时前
idea报错找不到XX包的解决方法
java·intellij-idea·启动报错·缺少包
Agent产品评测局2 小时前
生产排期与MES/ERP系统打通,实操方法详解 —— 2026企业级智能体自动化选型与实战指南
java·运维·人工智能·ai·chatgpt·自动化
阿丰资源3 小时前
基于Spring Boot的电影城管理系统(直接运行)
java·spring boot·后端
呱牛do it3 小时前
企业级门户网站设计与实现:基于SpringBoot + Vue3的全栈解决方案(Day 8)
java
小锋学长生活大爆炸4 小时前
【开源软件】这次iPhone也是用上Claw了 | PhoneClaw
ios·开源软件·iphone·claw