溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java的http請求工具類HttpClientUtils怎么使用

發(fā)布時(shí)間:2021-11-24 15:56:48 來源:億速云 閱讀:263 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“java的http請求工具類HttpClientUtils怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“java的http請求工具類HttpClientUtils怎么使用”吧!

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
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.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
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.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.util.CollectionUtils;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.net.URLEncoder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 
 */
public class HttpClientUtils {
    private static Log logger = LogFactory.getLog(HttpClientUtils.class);

    private static PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
    private static CloseableHttpClient httpClient = null;
    private static final int DEFAULT_MAX_TOTAL_CONNECTION = 1024;
    private static final int DEFAULT_MAX_PER_ROUTE = 300;
    private static final String DEFAULT_ENCODING = "UTF-8"; // 默認(rèn)的查詢參數(shù)及返回結(jié)果的字符串編碼
    private static final String CONTENT_TYPE_TEXT_XML = "application/xml"; // 默認(rèn)的查詢參數(shù)及返回結(jié)果的字符串編碼
    private static final String APPLICATION_XML = "text/xml"; // 默認(rèn)的查詢參數(shù)及返回結(jié)果的字符串編碼
    private static final int DEFAULT_CONNECTION_TIME_OUT = 60000; // 默認(rèn)連接超時(shí)時(shí)間,
    // 60秒
    private static final int DEFAULT_READ_TIME_OUT = 200000; // 默認(rèn)響應(yīng)超時(shí)時(shí)間, 60秒

    public static JSONObject custRespHeader = new JSONObject();

    static {
        poolingHttpClientConnectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTION);
        poolingHttpClientConnectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE);
        httpClient = HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).build();
    }

    private HttpClientUtils() {
    }

    /**
     * @param url
     * @param params
     * @param encode
     * @param connectTimeout
     * @param readTimeout
     * @return
     */
    public static String invokeGet(String url, Map<String, String> params, String encode, int connectTimeout,
                                   int readTimeout) {
        if (connectTimeout <= 0) {
            return null;
        }
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeout)
                .setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectTimeout).build();
        String responseString;
        String requestUrl;
        try {
            requestUrl = buildRequestUrl(url, params);
        } catch (UnsupportedEncodingException e) {
            logger.error("encode http get params error, params is " + params, e);
            return "";
        }
        HttpPost httpGet = new HttpPost(requestUrl);
        httpGet.setHeader("Connection", "close");
        httpGet.setConfig(requestConfig);
        responseString = doRequest(httpGet, encode);
        return responseString;
    }

    /**
     * @param requestUrl
     * @param params
     * @return
     */
    public static String invokePost(String requestUrl, Map<String, Object> params) {
        return invokePost(requestUrl, params, null, null, DEFAULT_ENCODING, DEFAULT_CONNECTION_TIME_OUT,
                DEFAULT_READ_TIME_OUT);
    }
    public static String invokePost(String requestUrl, Map<String, Object> params,Map<String, String> requestHeader) {
        return invokePost(requestUrl, params, requestHeader, null, DEFAULT_ENCODING, DEFAULT_CONNECTION_TIME_OUT,
                DEFAULT_READ_TIME_OUT);
    }

    /**
     * @param requestUrl
     * @param requestHeader
     * @param requestBody
     * @return
     */
    public static String invokePost(String requestUrl, Map<String, String> requestHeader, String requestBody) {
        return invokePost(requestUrl, null, requestHeader, requestBody, DEFAULT_ENCODING, DEFAULT_CONNECTION_TIME_OUT,
                DEFAULT_READ_TIME_OUT);
    }


    /**
     * @param requestUrl
     * @param params
     * @param requestHeader
     * @param requestBody
     * @param encode
     * @param connectTimeout
     * @param readTimeout
     * @return
     */
    public static String invokePost(String requestUrl, Map<String, Object> params, Map<String, String> requestHeader,
                                    String requestBody, String encode, int connectTimeout, int readTimeout) {
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeout)
                .setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectTimeout).build();
        String responseString;
        HttpPost httpPost = new HttpPost(requestUrl);
        httpPost.setConfig(requestConfig);

        if (MapUtils.isNotEmpty(requestHeader)) {
            for (Map.Entry<String, String> entry : requestHeader.entrySet()) {
                httpPost.addHeader(entry.getKey(), entry.getValue());
            }
        }

        buildPostParams(httpPost, params, requestBody, encode);
        responseString = doRequest(httpPost, encode);
        return responseString;
    }

    /**
     * @param httpPost
     * @param params
     * @param requestBody
     * @param encode
     */
    private static void buildPostParams(HttpPost httpPost, Map<String, Object> params, String requestBody,
                                        String encode) {
        try {
            if (MapUtils.isNotEmpty(params)) {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    String value = null;
                    if (entry.getValue() instanceof String) {
                        value = (String) entry.getValue();
                    } else {
                        value = JSON.toJSONString(entry.getValue());
                    }
                    nameValuePairs.add(new BasicNameValuePair(entry.getKey(), value));
                }

                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, encode));
            }

            if (StringUtils.isNotBlank(requestBody)) {
                httpPost.setEntity(new StringEntity(requestBody, encode));
            }

        } catch (UnsupportedEncodingException e) {
            logger.error("HttpClientUtils.buildPostParams error, params = " + params, e);
        }
    }

    /**
     * @param httpRequestBase
     * @param encode
     * @return
     */
    private static String doRequest(HttpRequestBase httpRequestBase, String encode) {
        String responseString = null;
        try {
            long start = System.currentTimeMillis();
            CloseableHttpResponse response = httpClient.execute(httpRequestBase);
            logger.info("HttpClientUtils Begin Invoke: " + httpRequestBase.getURI() + ", cost time "
                    + (System.currentTimeMillis() - start) + " ms");
            try {
                Header[] headers = response.getHeaders("isLogin");
                if(headers!=null && headers.length>0){
                    custRespHeader.put(headers[0].getName(),headers[0].getValue());
                }
                HttpEntity entity = response.getEntity();
                try {
                    if (entity != null) {
                        responseString = EntityUtils.toString(entity, encode);
                    }
                } finally {
                    if (entity != null) {
                        entity.getContent().close();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                logger.error(String.format("[HttpClientUtils.doRequest] get response error, url:%s",
                        httpRequestBase.getURI()), e);
                responseString = "";
            } finally {
                if (response != null) {
                    response.close();
                }
            }
        } catch (SocketTimeoutException e) {
            e.printStackTrace();
            logger.error(String.format("[HttpClientUtils.doRequest] invoke get timout error, url:%s",
                    httpRequestBase.getURI()), e);
            responseString = "";
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(
                    String.format("[HttpClientUtils.doRequest] invoke get error, url:%s", httpRequestBase.getURI()), e);
            responseString = "";
        } finally {
            httpRequestBase.releaseConnection();
        }
        logger.info("HttpClientUtils response : " + responseString);

        return responseString;
    }

    /**
     * @param url
     * @param params
     * @return
     * @throws UnsupportedEncodingException
     */
    private static String buildRequestUrl(String url, Map<String, String> params) throws UnsupportedEncodingException {
        if (CollectionUtils.isEmpty(params)) {
            return url;
        }
        StringBuilder requestUrl = new StringBuilder();
        requestUrl.append(url);
        int i = 0;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (i == 0) {
                requestUrl.append("?");
            }
            requestUrl.append(entry.getKey());
            requestUrl.append("=");
            String value = entry.getValue();
            requestUrl.append(URLEncoder.encode(value, "UTF-8"));
            requestUrl.append("&");
            i++;
        }
        requestUrl.deleteCharAt(requestUrl.length() - 1);
        return requestUrl.toString();
    }


    public static void setStringParams(HttpPost httpost,
                                       Map<String, String> params) {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        Set<String> keySet = params.keySet();
        for (String key : keySet) {
            nvps.add(new BasicNameValuePair(key, params.get(key).toString()));
        }
        try {
            httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
        }
    }


    /**
     * @param httpRequest
     * @param entity
     * @param
     * @return
     */
    private static String sendRequest(HttpRequestBase httpRequest, HttpEntity entity) {

        httpRequest.setHeader("User-Agent", "okHttp");
        if (httpRequest instanceof HttpEntityEnclosingRequestBase) {
//	            checkArgument(null!=entity,"HttpEntity請求體不能為空");
            ((HttpEntityEnclosingRequestBase) httpRequest).setEntity(entity);
        }

        //忽略證書
        SSLContext sslContext = SSLContexts.createDefault();

        X509TrustManager tm = new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] xcs,
                                           String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs,
                                           String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        try {
            sslContext.init(null, new TrustManager[]{tm}, null);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        X509HostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);

        Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("https", sslsf)
                .build();

        HttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(r);
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();


        CloseableHttpResponse response = null;
        String resString = null;
        try {
            response = httpClient.execute(httpRequest);
            HttpEntity resEntity = response.getEntity();
            int statusCode = response.getStatusLine().getStatusCode();
            resString = EntityUtils.toString(resEntity, "UTF-8");
            System.err.println(statusCode);
            if (statusCode != HttpStatus.SC_OK) {
                System.out.println("響應(yīng)碼狀態(tài)不是200");
            }
            return resString;
        } catch (Exception e) {
            throw new RuntimeException(resString, e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (httpRequest != null) {
                    httpRequest.releaseConnection();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

}

調(diào)用示例:

public static void delAll(){
        String url = path+"test/test/deleteAll";
        Map map = new HashMap<>();
        String str = HttpClientUtils.invokePost(url,map);
        System.out.println("delAll.str="+str);
    }

到此,相信大家對“java的http請求工具類HttpClientUtils怎么使用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI