博主
258
258
258
258
专辑

第二节 第三方接口访问:使用hutool工具类实现http请求

亮子 2023-05-17 00:27:52 7233 0 0 0

1、添加依赖

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.11</version>
        </dependency>

2、请求示例代码

package com.shenma2009;

import org.junit.jupiter.api.Test;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 军哥
 * @version 1.0
 * @description: TestHutoolHttpApp
 * @date 2023/5/15 16:25
 */

public class TestHutoolHttpApp {
    /**
     * @description 使用GET方法进行无参请求示例
     * @author 军哥
     * @date 2023/5/15 16:33
     * @version 1.0
     */
    @Test
    public void test01() {
        String getResult = HttpUtil.createGet("https://www.shenmazong.com/test/getRandCode")
                //这个请求头.header是自己项目需要加的,可以省略
                .header("Authorzation", "Basic dG9uZ2Z1bNsb3VkX3VpOnRvbmdmdW5jbG91ZF91aV9zZWNyZXQ=")
                .execute()
                //返回参数格式utf-8
                .charset("utf-8")
                .body();
        System.out.println(getResult);
    }

    /**
     * @description 使用GET方法在路径中传参示例
     * @author 军哥
     * @date 2023/5/15 16:34
     * @version 1.0
     */

    @Test
    public void test02() {
        String getResult = HttpUtil.createGet("https://www.shenmazong.com/test/sendSms/1/qw")
                //这个请求头.header是自己项目需要加的,可以省略
                .header("Authorzation", "Basic dG9uZ2Z1bNsb3VkX3VpOnRvbmdmdW5jbG91ZF91aV9zZWNyZXQ=")
                .execute()
                //返回参数格式utf-8
                .charset("utf-8")
                .body();
        System.out.println(getResult);
    }

    /**
     * @description 使用GET方法在路径中拼接参数
     * @author 军哥
     * @date 2023/5/17 8:18
     * @version 1.0
     */
    @Test
    public void test03() {
        String getResult = HttpUtil.createGet("https://shenmazong.com/test/getMd5?message='哈哈哈'")
                //这个请求头.header是自己项目需要加的,可以省略
                .header("Authorzation", "Basic dG9uZ2Z1bNsb3VkX3VpOnRvbmdmdW5jbG91ZF91aV9zZWNyZXQ=")
                .execute()
                //返回参数格式utf-8
                .charset("utf-8")
                .body();
        System.out.println("getMd5结果打印 :"+getResult);
    }

    /**
     * @description 通过POST方法发送JSON格式的参数
     * @author 军哥
     * @date 2023/5/17 8:19
     * @version 1.0
     */

    @Test
    public void test04() {
        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.set("userId", 1);
        jsonObject.set("message", "123");
        String postResult = HttpUtil.createPost("https://shenmazong.com/test/sendEmail")
                //这个请求头.header是自己项目需要加的,可以省略
                .header("Content-Type", "application/json")
                //这两个请求头是项目需要加的,可以省略
                .header("Authorization", "Basic dG9u1ZF91aV9zZWNyZXQ=")
                .header("tenant", "MD")
                //传输参数
                .body(jsonObject.toString())
                .execute()
                .body();
        System.out.println("sendEmail结果打印:"+postResult);
    }

    /**
     * @description 通过POST方法发送form表单格式的参数
     * @author 军哥
     * @date 2023/5/17 8:24
     * @version 1.0
     */

    @Test
    public void test05() {
        String url = "https://shenmazong.com/test/login";//指定URL
        Map<String, Object> map = new HashMap<>();//存放参数
        map.put("username", "andy");
        map.put("userpass", "12345");
        HashMap<String, String> headers = new HashMap<>();//存放请求头,可以存放多个请求头
        //发送post请求并接收响应数据
        String result = HttpUtil.createPost(url).form(map).execute().body();
        System.out.println("login"+result);
    }
}