HttpUtils工具类

源码地址:HttpUtils工具类-Java文档类资源-CSDN下载


package com.hx.platform.dxjfgl.tools;


import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.servlet.ServletInputStream;
import java.io.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;

/**
 * HTTP 请求工具类
 */
public class HttpUtils {

    private final static Logger LOGGER = LoggerFactory.getLogger(HttpUtils.class);

    private static PoolingHttpClientConnectionManager connMgr;
    private static RequestConfig requestConfig;
    private static final int MAX_TIMEOUT = 7000;
    private static final String CONTENT_TYPE_JSON = "application/json";
    private static final String CONTENT_TYPE_URLENCODE = "application/x-www-form-urlencoded";

    static {
        // 设置连接池  
        connMgr = new PoolingHttpClientConnectionManager();
        // 设置连接池大小  
        connMgr.setMaxTotal(100);
        connMgr.setDefaultMaxPerRoute(connMgr.getMaxTotal());
        RequestConfig.Builder configBuilder = RequestConfig.custom();
        // 设置连接超时  
        configBuilder.setConnectTimeout(MAX_TIMEOUT);
        // 设置读取超时  
        configBuilder.setSocketTimeout(MAX_TIMEOUT);
        // 设置从连接池获取连接实例的超时  
        configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT);
        // 在提交请求之前 测试连接是否可用  
        configBuilder.setStaleConnectionCheckEnabled(true);
        requestConfig = configBuilder.build();
    }

    /**
     * 发送 GET 请求(HTTP),不带输入数据
     *
     * @param url
     * @return
     */
    public static String doGet(String url) {
        return doGet(url, new HashMap());
    }

    /**
     * 发送 GET 请求(HTTP),K-V形式
     *
     * @param url
     * @param params
     * @return
     */
    public static String doGet(String url, Map params) {
        String apiUrl = url;
        String token = "";
        if (StringUtils.isNotEmpty((CharSequence) params.get("token"))) {
            token = (String) params.get("token");
            params.remove("token");
        }
        StringBuffer param = new StringBuffer();
        int i = 0;
        for (String key : params.keySet()) {
            if (i == 0)
                param.append("?");
            else
                param.append("&");
            param.append(key).append("=").append(params.get(key));
            i++;
        }
        apiUrl += param;
        String result = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpPost = new HttpGet(apiUrl);
            if (StringUtils.isNotEmpty(token)) {
                httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
                httpPost.setHeader("token", "Bearer " + token);
            }
            HttpResponse response = httpclient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();

            System.out.println("执行状态码 : " + statusCode);

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                result = IOUtils.toString(instream, "UTF-8");
            }
        } catch (IOException e) {
            LOGGER.info("发送 GET 请求(HTTP),K-V形式", e);
        }
        return result;
    }

    /**
     * @return java.lang.String
     * @Author guxiang
     * @Date 2021-11-25 17:33
     * @Param [url, jsonStr, token]
     * @Description put请求
     */
    public static String doPut(String url, String jsonStr, String token) {
        String result = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPut httpPut = new HttpPut(url);
            if (StringUtils.isNotEmpty(token)) {
                httpPut.setHeader("Content-Type", "application/json;charset=UTF-8");
                httpPut.setHeader("token", "Bearer " + token);
            }
            httpPut.setConfig(requestConfig);
            StringEntity stringEntity = new StringEntity(jsonStr, "UTF-8");//解决中文乱码问题
            stringEntity.setContentEncoding("UTF-8");
            stringEntity.setContentType(CONTENT_TYPE_JSON);
            httpPut.setEntity(stringEntity);
            HttpResponse response = httpclient.execute(httpPut);
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("执行状态码 : " + statusCode);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                result = IOUtils.toString(instream, "UTF-8");
            }
        } catch (IOException e) {
            LOGGER.info("发送 GET 请求(HTTP),K-V形式", e);
        }
        return result;
    }

    /**
     * 获取本机IP地址
     *
     * @param
     * @return
     */
    public static InetAddress getInetAddress() throws UnknownHostException {
        try {
            InetAddress candidateAddress = null;
            // 遍历所有的网络接口
            for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) {
                NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
                // 在所有的接口下再遍历IP
                for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) {
                    InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
                    if (!inetAddr.isLoopbackAddress()) {// 排除loopback类型地址
                        if (inetAddr.isSiteLocalAddress()) {
                            // 如果是site-local地址,就是它了
                            return inetAddr;
                        } else if (candidateAddress == null) {
                            // site-local类型的地址未被发现,先记录候选地址
                            candidateAddress = inetAddr;
                        }
                    }
                }
            }
            if (candidateAddress != null) {
                return candidateAddress;
            }
            // 如果没有发现 non-loopback地址.只能用最次选的方案
            InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
            if (jdkSuppliedAddress == null) {
                throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");
            }
            return jdkSuppliedAddress;
        } catch (Exception e) {
            UnknownHostException unknownHostException = new UnknownHostException(
                    "Failed to determine LAN address: " + e);
            unknownHostException.initCause(e);
            throw unknownHostException;
        }
    }

    /**
     * 发送 POST 请求(HTTP),不带输入数据
     *
     * @param apiUrl
     * @return
     */
    public static String doPost(String apiUrl) {
        return doPost(apiUrl, new HashMap());
    }

    /**
     * 发送 POST 请求(HTTP),K-V形式
     *
     * @param apiUrl API接口URL
     * @param params 参数map
     * @return
     */
    public static String doPost(String apiUrl, Map params) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String httpStr = null;
        HttpPost httpPost = new HttpPost(apiUrl);
        CloseableHttpResponse response = null;

        try {
            httpPost.setConfig(requestConfig);
            List pairList = new ArrayList<>(params.size());
            for (Map.Entry entry : params.entrySet()) {
                NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
                pairList.add(pair);
            }
            httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8")));
            response = httpClient.execute(httpPost);
            System.out.println(response.toString());
            HttpEntity entity = response.getEntity();
            httpStr = EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            LOGGER.info("发送 GET 请求(HTTP),K-V形式", e);
        } finally {
            if (response != null) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return httpStr;
    }

    /**
     * 发送 POST 请求(HTTP),K-V形式
     *
     * @param apiUrl API接口URL
     * @param params 参数map
     * @return
     */
    public static String doPostMax(String apiUrl, Map params) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String httpStr = null;
        HttpPost httpPost = new HttpPost(apiUrl);
        CloseableHttpResponse response = null;
        String token = (String) params.get("token");
        LOGGER.info("请求token:[{}]", token);
        if (StringUtils.isNotEmpty((token))) {
            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
            httpPost.setHeader("Mars-Auth", "Bearer " + token);
        }
        try {
            httpPost.setConfig(requestConfig);
            params.remove("token");
            String jsonStr = JSONObject.toJSONString(params);
            LOGGER.info("HTTP请求数据JSON:[{}]", jsonStr);
            StringEntity stringEntity = new StringEntity(jsonStr, "UTF-8");//解决中文乱码问题
            stringEntity.setContentEncoding("UTF-8");
            stringEntity.setContentType(CONTENT_TYPE_JSON);

            httpPost.setEntity(stringEntity);
            response = httpClient.execute(httpPost);
            System.out.println(response.toString());
            HttpEntity entity = response.getEntity();
            httpStr = EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            LOGGER.info("发送 GET 请求(HTTP),K-V形式", e);
        } finally {
            if (response != null) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return httpStr;
    }

    /**
     * 发送 POST 请求(HTTP),JSON形式
     *
     * @param apiUrl
     * @param
     * @return
     */
    public static String doPost(String apiUrl, String jsonStr) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String httpStr = null;
        HttpPost httpPost = new HttpPost(apiUrl);
        CloseableHttpResponse response = null;

        try {
            httpPost.setConfig(requestConfig);
            StringEntity stringEntity = new StringEntity(jsonStr, "UTF-8");//解决中文乱码问题
            stringEntity.setContentEncoding("UTF-8");
            stringEntity.setContentType(CONTENT_TYPE_JSON);
            httpPost.setEntity(stringEntity);
            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            System.out.println(response.getStatusLine().getStatusCode());
            httpStr = EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            LOGGER.info("发送 POST 请求(HTTP),JSON形式异常!", e);
        } finally {
            if (response != null) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return httpStr;
    }

    /**
     * 发送 POST 请求(HTTP),JSON形式
     *
     * @param apiUrl
     * @param
     * @return
     */
    public static String doPost(String apiUrl, String jsonStr, String token) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String httpStr = null;
        HttpPost httpPost = new HttpPost(apiUrl);
        CloseableHttpResponse response = null;

        if (StringUtils.isNotEmpty(token)) {
            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
            httpPost.setHeader("token", "Bearer " + token);
        }

        try {
            httpPost.setConfig(requestConfig);
            StringEntity stringEntity = new StringEntity(jsonStr, "UTF-8");//解决中文乱码问题
            stringEntity.setContentEncoding("UTF-8");
            stringEntity.setContentType(CONTENT_TYPE_JSON);
            httpPost.setEntity(stringEntity);
            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            System.out.println(response.getStatusLine().getStatusCode());
            httpStr = EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            LOGGER.info("发送 POST 请求(HTTP),JSON形式异常!", e);
        } finally {
            if (response != null) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return httpStr;
    }

    /**
     * 发送 SSL POST 请求(HTTPS),K-V形式
     *
     * @param apiUrl API接口URL
     * @param params 参数map
     * @return
     */
    public static String doPostSSL(String apiUrl, Map params) {
        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
        HttpPost httpPost = new HttpPost(apiUrl);
        CloseableHttpResponse response = null;
        String httpStr = null;

        try {
            httpPost.setConfig(requestConfig);
            List pairList = new ArrayList(params.size());
            for (Map.Entry entry : params.entrySet()) {
                NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry
                        .getValue().toString());
                pairList.add(pair);
            }
            httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("utf-8")));
            response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                return null;
            }
            HttpEntity entity = response.getEntity();
            if (entity == null) {
                return null;
            }
            httpStr = EntityUtils.toString(entity, "utf-8");
        } catch (Exception e) {
            LOGGER.info("发送 GET 请求(HTTP),K-V形式", e);
        } finally {
            if (response != null) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return httpStr;
    }

    /**
     * 发送 SSL POST 请求(HTTPS),JSON形式
     *
     * @param apiUrl API接口URL
     * @param json   JSON对象
     * @return
     */
    public static String doPostSSL(String apiUrl, Object json, String contentType) {
        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
        HttpPost httpPost = new HttpPost(apiUrl);
        CloseableHttpResponse response = null;
        String httpStr = null;

        try {
            httpPost.setConfig(requestConfig);
            StringEntity stringEntity = new StringEntity(json.toString(), "UTF-8");//解决中文乱码问题
            stringEntity.setContentEncoding("UTF-8");
            stringEntity.setContentType(contentType);
            httpPost.setEntity(stringEntity);
            response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                return null;
            }
            HttpEntity entity = response.getEntity();
            if (entity == null) {
                return null;
            }
            httpStr = EntityUtils.toString(entity, "utf-8");
        } catch (Exception e) {
            LOGGER.info("发送 SSL POST 请求(HTTPS),JSON形式异常!", e);
        } finally {
            if (response != null) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return httpStr;
    }

    /**
     * 创建SSL安全连接
     *
     * @return
     */
    private static SSLConnectionSocketFactory createSSLConnSocketFactory() {
        SSLConnectionSocketFactory sslsf = null;
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {

                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();
            sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() {

                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }

                @Override
                public void verify(String host, SSLSocket ssl) throws IOException {
                }

                @Override
                public void verify(String host, X509Certificate cert) throws SSLException {
                }

                @Override
                public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
                }
            });
        } catch (GeneralSecurityException e) {
            LOGGER.info("创建SSL安全连接异常!", e);
        }
        return sslsf;
    }


    /**
     * 测试方法
     *
     * @param args
     */
    public static void main(String[] args) throws Exception {
//        String kdUrl = "http://139.8.4.220";
//        String projUrl = "http://139.0.25.34:8090/oa";
//        // 先登录获取token
//        Map loginMap = new HashMap();
//        loginMap.put("username", "hexin");
//        loginMap.put("password", Base64.encode("hexin@123"));
//        loginMap.put("confirmed", 1);
//        JSONObject loginParam = JSONObject.fromObject(loginMap);
//        Map headMap = new HashMap<>();
//        Header[] headers = com.hx.platform.oa.commons.utils.HttpUtils.returnPostHeaders(kdUrl + "/login", loginParam.toString(), headMap);
//        for (Header header : headers) {
//
//        }
//        // 调用订阅
//        String myUrl = projUrl + "/openAttence/getAttenceByKD.do";
//        String type = "0";//类型(0人脸信息;1报警信息)
//        Map map = new HashMap();
//        map.put("url", myUrl);
//        map.put("type", type);
        map.put("userData", token);
//        JSONObject fromObject = JSONObject.fromObject(map);
        headMap.put("Cookie", token);
        headMap.put("Content-Type", "application/json;charset=UTF-8");
//        String forBody = com.hx.platform.oa.commons.utils.HttpUtils.postBody(kdUrl + "/tm/service/thrid-module/addSubscribe", fromObject.toString(), headMap);
//        System.out.println(forBody);
    }

    public static StringBuilder getInputStreamReaderByCode(ServletInputStream servletInputStream, String code) throws IOException {
        String jsonStr = null;
        StringBuilder result = new StringBuilder();
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        try {
            inputStreamReader = new InputStreamReader(servletInputStream, code);
            reader = new BufferedReader(inputStreamReader);
            while ((jsonStr = reader.readLine()) != null) {
                result.append(jsonStr);
            }
        } catch (IOException e) {
            LOGGER.info("HttpUtils.getInputStreamReaderByCode异常!", e);
        } finally {
            assert inputStreamReader != null;
            inputStreamReader.close();
            assert reader != null;
            reader.close();
        }
        return result;
    }


    /**
     * 发送httppost请求
     *
     * @param url
     * @param
     * @throws Exception
     */
    public static String postBody(String url, String body, Map headMap) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost request = new HttpPost(url);
        CloseableHttpResponse response = null;
        HttpEntity httpEntity = null;
        try {
            request.setHeader("Content-Type", "application/json");
            if (headMap != null && headMap.size() > 0) {
                Iterator it = headMap.keySet().iterator();
                while (it.hasNext()) {
                    String headKey = it.next();
                    request.addHeader(headKey, headMap.get(headKey));
                }
            }
            if (body != null) {
                StringEntity se = new StringEntity(body, "UTF-8");
                request.setEntity(se);
            }

            response = httpClient.execute(request);
            //获得状态码
            httpEntity = response.getEntity();
            return EntityUtils.toString(httpEntity);
        } catch (Exception e) {
            throw e;
        } finally {
            if (httpEntity != null) {
                EntityUtils.consume(httpEntity);
            }
            if (response != null) {
                response.close();
            }
            httpClient.close();
        }
    }

    /**
     * 登录成功返回headers
     *
     * @param url
     * @param
     * @throws Exception
     */
    public static Header[] returnPostHeaders(String url, String body, Map headMap) throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();
//        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost request = new HttpPost(url);
        CloseableHttpResponse response = null;
        HttpEntity httpEntity = null;
        try {
            request.setHeader("Content-Type", "application/json");
            if (headMap != null && headMap.size() > 0) {
                Iterator it = headMap.keySet().iterator();
                while (it.hasNext()) {
                    String headKey = it.next();
                    request.addHeader(headKey, headMap.get(headKey));
                }
            }
            if (body != null) {
                StringEntity se = new StringEntity(body, "UTF-8");
                request.setEntity(se);
            }

            response = httpClient.execute(request);

            //获得状态码
            return response.getAllHeaders();
        } catch (Exception e) {
            throw e;
        } finally {
            if (httpEntity != null) {
                EntityUtils.consume(httpEntity);
            }
            if (response != null) {
                response.close();
            }
            httpClient.close();
        }
    }


    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }
}

你可能感兴趣的:(java,https,http)