Java hutool.HttpRequest实现get和post

private static void testPost() {
        String url = "http://xxx";
        String body = "{}";

        HttpRequest httpRequest = HttpRequest.post(url).body(body);
        httpRequest.header("Content", "application/json");
        httpRequest.header("Authorization", "Basic Zxxxy");
        String res = httpRequest.execute().body();
        log.info(res);
    }

    private static void testGet() {
        String url = "http://xxx";
        HttpRequest httpRequest = HttpRequest.get(url);
        String res = httpRequest.execute().body();
        log.info(res);
    }
<!-- hutool https://www.hutool.cn/docs/#/ -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.5</version>
        </dependency>

 

posted @ 2023-07-19 16:14  都是城市惹的祸  阅读(1905)  评论(0)    收藏  举报