当前位置: 首页 > 工具软件 > Unirest > 使用案例 >

基于Unirest的Http请求工具类

姜泳
2023-12-01
package com.mes.util.common;

import com.alibaba.fastjson.JSON;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;

/**
 * HTTP请求工具类
 *
 * @author xuweijun
 * @date 2022/6/21
 */
public class HttpUtil {
    /**
     * private Constructor
     */
    private HttpUtil() {
    }

    /**
     * post请求
     *
     * @param url     请求地址
     * @param jsonStr 请求数据,json字符串
     * @return java.lang.String
     * @author xuweijun
     * @date 2022/6/21 15:09
     */
    public static String sendPost(final String url, final String jsonStr) {
        HttpResponse<String> response = Unirest.post(url)
                .header("Content-Type", "application/json")
                .body(jsonStr)
                .asString();
        return response.getBody();
    }

    /**
     * post请求
     *
     * @param url    请求地址
     * @param object 请求数据
     * @return java.lang.String
     * @author xuweijun
     * @date 2022/6/21 15:09
     */
    public static String sendPost(final String url, final Object object) {
        return sendPost(url, JSON.toJSONString(object));
    }

    public static String sendGet(final String url) {
        HttpResponse<String> response = Unirest.get(url)
                .asString();
        return response.getBody();
    }

    /**
     * post请求
     *
     * @param url
     * @param jsonStr
     * @return kong.unirest.JsonNode
     * @author xuweijun
     * @date 2022/6/21 15:14
     */
    public static JsonNode sendPostAsJson(final String url, final String jsonStr) {
        HttpResponse<JsonNode> response = Unirest.post(url)
                .header("Content-Type", "application/json")
                .body(jsonStr)
                .asJson();
        return response.getBody();
    }

    /**
     * post请求
     *
     * @param url
     * @param object
     * @return kong.unirest.JsonNode
     * @author xuweijun
     * @date 2022/6/21 15:14
     */
    public static JsonNode sendPostAsJson(final String url, final Object object) {
        return sendPostAsJson(url, JSON.toJSONString(object));
    }

    /**
     * 发送Get请求
     *
     * @param url
     * @return kong.unirest.JsonNode
     * @author xuweijun
     * @date 2022/6/21 15:16
     */
    public static JsonNode sendGetAsJson(final String url) {
        HttpResponse<JsonNode> response = Unirest.get(url)
                .asJson();
        return response.getBody();
    }

    /**
     * 发送Get请求
     *
     * @param url
     * @param clazz
     * @return T
     * @author xuweijun
     * @date 2022/6/21 15:16
     */
    public static <T> T sendGetAsObject(final String url, Class<? extends T> clazz) {
        HttpResponse<T> response = Unirest.get(url)
                .asObject(clazz);
        return response.getBody();
    }

    /**
     * 发送Post请求
     *
     * @param url
     * @param jsonStr
     * @param clazz
     * @return T
     * @author xuweijun
     * @date 2022/6/21 15:17
     */
    public static <T> T sendPostAsObject(final String url, final String jsonStr, Class<? extends T> clazz) {
        HttpResponse<T> response = Unirest.post(url)
                .header("Content-Type", "application/json")
                .body(jsonStr)
                .asObject(clazz);
        return response.getBody();
    }

    /**
     * 发送Post请求
     *
     * @param url
     * @param object
     * @param clazz
     * @return T
     * @author xuweijun
     * @date 2022/6/21 15:17
     */
    public static <T> T sendPostAsObject(final String url, final Object object, Class<? extends T> clazz) {
        return sendPostAsObject(url, JSON.toJSONString(object), clazz);
    }
}

 类似资料: