All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qingxun.javasdkapi.utils.IHttpUtil Maven / Gradle / Ivy

package com.qingxun.javasdkapi.utils;

import lombok.Data;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


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

public interface IHttpUtil {
    Logger log = LoggerFactory.getLogger(IHttpUtil.class);

    @Data
    class HttpFileResponse {
        private byte[] bytes;
        private Map headers;
        private String body;
    }

    /**
     * @param url         请求路径
     * @param parameters  请求参数
     * @param fileContent 请求文件内容
     * @param fileName    请求文件名称
     * @return
     * @throws IOException
     */
    String post(String url, TreeMap parameters, byte[] fileContent, String fileName) throws IOException;

    String post(String url, TreeMap parameters, File file);

    String post(String url, TreeMap parameter);

    HttpFileResponse formResponseStream(String url, TreeMap parameter);


    default void close(CloseableHttpResponse response) {
        if (response == null) {
            log.error("CloseableHttpResponse为null,无法关闭");
            return;
        }
        try {
            response.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("CloseableHttpResponse关闭失败");


        }
    }

    default void releaseConnection(HttpPost httpPost) {
        if (httpPost == null) {
            log.error("HttpPost为null,无法释放连接");
            return;
        }
        try {
            httpPost.releaseConnection();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("HttpPost释放连接失败");
        }
    }

    default void clientClose(CloseableHttpClient httpClient) {

        if (httpClient == null) {
            log.error("CloseableHttpCliente为null,无法关闭");
            return;
        }
        try {
            httpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("CloseableHttpClient关闭失败");

        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy