java-Http请求类

package com.dm.frame.application.common.util;
 
import com.dm.frame.application.common.exception.HttpRequestException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
 
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.util.Map;
 
@Slf4j
public class HttpUtil {
 
    private static final int MAX_TOTAL = 600;
    private static final int MAX_PER_ROUTE = 300;
    private static final int CONNECT_TIMEOUT = 6000;
    private static final int SOCKET_TIMEOUT = 60000;
 
    // 连接池管理器
    private static PoolingHttpClientConnectionManager connectionManager = null;
 
    // 连接实例
    private static CloseableHttpClient httpClient;
 
    static {
        try {
            log.info("httpClient连接池初始化,连接池大小:{},每个路由最大连接数:{}", MAX_TOTAL, MAX_PER_ROUTE);
            //跳过SSL证书认证策略
            SSLConnectionSocketFactory sslFactory = createMySSLConnectionSocketFactory();
            // 配置同时支持 HTTP 和 HTTPS
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", sslFactory).build();
            // 创建连接池管理器对象
            connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
            // 最大连接数
            connectionManager.setMaxTotal(MAX_TOTAL);
            // 每个路由最大连接数
            connectionManager.setDefaultMaxPerRoute(MAX_PER_ROUTE);
            // 初始化httpClient
            httpClient = createHttpClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 创建HttpClient对象
     *
     * @return CloseableHttpClient
     */
    private static CloseableHttpClient createHttpClient() {
        if (httpClient == null) {
            log.info("初始化httpClient, 连接服务器超时时间:{}, 获取数据的超时时间:{}", CONNECT_TIMEOUT, SOCKET_TIMEOUT);
            RequestConfig defaultRequestConfig = RequestConfig.custom()
                    .setConnectTimeout(CONNECT_TIMEOUT) // 设置服务器超时时间
                    .setSocketTimeout(SOCKET_TIMEOUT) // 设定获取数据的超时时间
                    .build();
 
            httpClient = HttpClients.custom()
                    .setDefaultRequestConfig(defaultRequestConfig)
                    .setConnectionManager(connectionManager)
                    .setConnectionManagerShared(true)
//                .evictExpiredConnections()
                    .build();
        }
        return httpClient;
    }
 
    // 设置header
    private static <T extends HttpRequestBase> void setFullHeaders(T httpRequest, Map<String, String> addHeaders) {
        httpRequest.setHeader("Content-Type", "application/json");
        if (addHeaders.size() > 0) {
            for (String key : addHeaders.keySet()) {
                httpRequest.addHeader(key, addHeaders.get(key));
            }
        }
    }
 
    // Http协议Get请求
    public static String httpsGet(String url, Map<String, String> headers) throws Exception, HttpRequestException {
        CloseableHttpResponse response = null;
        String result = "";
        try {
            httpClient = createHttpClient();
            HttpGet httpGet = new HttpGet(url);
            //设置header
            setFullHeaders(httpGet, headers);
            //发起请求
            response = httpClient.execute(httpGet);
            //处理返回结果
            if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 300) {
                throw new HttpRequestException(result);
            }
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity, "UTF-8");
            }
            EntityUtils.consume(entity);
            httpGet.releaseConnection();
        } catch (Exception e) {
            e.printStackTrace();
            throw new HttpRequestException("httpclient IO error!", "42000");
        } finally {
            close(response);
        }
        return result;
    }
 
    // Http协议Post请求
    public static String httpsPost(String url, String json, Map<String, String> headers) throws HttpRequestException {
        CloseableHttpResponse response = null;
        String result = "";
        try {
            httpClient = createHttpClient();
            HttpPost httpPost = new HttpPost(url);
            //设置header
            setFullHeaders(httpPost, headers);
            //填充参数
            httpPost.setEntity(new StringEntity(json, "utf-8"));
            //发起请求
            response = httpClient.execute(httpPost);
            //处理返回结果
            if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 300) {
                throw new HttpRequestException(result);
            }
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity, "UTF-8");
            }
            EntityUtils.consume(entity);
        } catch (Exception e) {
            e.printStackTrace();
            throw new HttpRequestException("httpclient IO error!", "42000");
        } finally {
            close(response);
        }
        return result;
    }
 
    // Http协议Put请求
    public static String httpsPut(String url, String body, Map<String, String> headers) throws HttpRequestException {
        CloseableHttpResponse response = null;
        String result = "";
        try {
            httpClient = createHttpClient();
            HttpPut httpRequest = new HttpPut(url);
            //设置header
            setFullHeaders(httpRequest, headers);
            //填充参数
            httpRequest.setEntity(new StringEntity(body, "utf-8"));
            //发起请求
            response = httpClient.execute(httpRequest);
            //处理返回结果
            if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 300) {
                throw new HttpRequestException(result);
            }
            //解析实体
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity, "UTF-8");
            }
            EntityUtils.consume(entity);
        } catch (Exception e) {
            e.printStackTrace();
            throw new HttpRequestException("httpclient IO error!", "42000");
        } finally {
            close(response);
        }
        return result;
    }
 
    // Http协议Delete请求
    public static String httpsDelete(String url, Map<String, String> headers) throws HttpRequestException {
        CloseableHttpResponse response = null;
        String result = "";
        try {
            httpClient = createHttpClient();
            HttpDelete httpRequest = new HttpDelete(url);
            //设置header
            setFullHeaders(httpRequest, headers);
            //发起请求
            response = httpClient.execute(httpRequest);
            //处理返回结果
            if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 300) {
                throw new HttpRequestException(result);
            }
            HttpEntity entity = response.getEntity();
            //返回结果可能为空
            if (entity != null) {
                result = EntityUtils.toString(entity, "UTF-8");
            }
            EntityUtils.consume(entity);
        } catch (Exception e) {
            e.printStackTrace();
            throw new HttpRequestException("httpclient IO error!", "42000");
        } finally {
            close(response);
        }
        return result;
    }
 
    //创建一个跳过SSL证书认证策略的简单连接
    public static CloseableHttpClient createSSLClientDefault() throws Exception {
        //信任所有
        SSLContext sslcontext = new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
        return HttpClients.custom().setSSLSocketFactory(sslsf).build();
    }
 
    // 跳过SSL证书认证策略
    private static SSLConnectionSocketFactory createMySSLConnectionSocketFactory() throws Exception {
        SSLContext sslcontext = new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build();
        return new SSLConnectionSocketFactory(sslcontext);
    }
 
 
    /**
     * 关闭CloseableHttpResponse对象
     *
     * @param response CloseableHttpResponse对象
     */
    private static void close(CloseableHttpResponse response) {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农汉子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值