package com.tofly.base.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.tofly.common.core.entity.ResultRespone;

import java.io.IOException;
import java.util.Map;

/**
 * http工具类
 *
 * @author zev
 * 2022/01/07
 * <p>
 * HttpClientUtil
 */
public class HttpClientUtil {

    /**
     * 编码格式,发送编码格式统一用UTF-8
     */
    private static final String ENCODING = "UTF-8";

    /**
     * 设置连接超时时间,单位毫秒
     */
    private static final int CONNECT_TIMEOUT = 3 * 60 * 1000;

    /**
     * 请求响应超时时间,单位毫秒
     */
    private static final int SOCKET_TIMEOUT = 3 * 60 * 1000;

    /**
     * 发送get请求;带请求头和请求参数
     */
    public static HttpResponse doGet(String url, Map<String, String> headers, Map<String, Object> params)  {
        //链式构建请求
        HttpResponse execute = HttpRequest.get(url)
                .addHeaders(headers)
                .form(params)
                .timeout(CONNECT_TIMEOUT)
                .execute();

        // 执行请求获取响应体并释放资源
        return execute;
    }

    public static HttpResponse doPost(String url, Map<String, String> headers, Map<String, Object> params)  {
        HttpResponse execute = HttpRequest.post(url)
                .addHeaders(headers)
                //表单内容
                .form(params)
                //超时，毫秒
                .timeout(CONNECT_TIMEOUT)
                .execute();

        // 执行请求获取响应体并释放资源
        return execute;
    }
}

