日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

HttpClientUtils 工具類

 修行的嘟嘟 2023-09-25

添加依賴

<dependency>

      <groupId>org.apache.httpcomponents</groupId>

      <artifactId>httpclient</artifactId>

      <version>4.5.5</version>

    </dependency>

代碼

package com.utils;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import org.apache.http.ParseException;

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.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

/**

 * @author 余勝軍

 * @ClassName HttpClients

 * @qq 644064779

 * @addres www.mayikt.com

 * 微信:yushengjun644

 */

public class HttpClientUtils {

    private static final CloseableHttpClient httpClient;

    public static final String CHARSET = "UTF-8";

    private static final Log log = LogFactory.getLog(HttpClientUtils.class);

    // 采用靜態(tài)代碼塊,初始化超時(shí)時(shí)間配置,再根據(jù)配置生成默認(rèn)httpClient對(duì)象

    static {

        RequestConfig config = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(15000).build();

        httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();

    }

    public static String doGet(String url, Map<String, String> params) {

        return doGet(url, params, CHARSET);

    }

    public static String doPost(String url, Map<String, String> params) throws IOException {

        return doPost(url, params, CHARSET);

    }

    /**

     * HTTP Get 獲取內(nèi)容

     *

     * @param url     請(qǐng)求的url地址 ?之前的地址

     * @param params  請(qǐng)求的參數(shù)

     * @param charset 編碼格式

     * @return 頁(yè)面內(nèi)容

     */

    public static String doGet(String url, Map<String, String> params, String charset) {

        try {

            if (params != null && !params.isEmpty()) {

                List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());

                for (Map.Entry<String, String> entry : params.entrySet()) {

                    String value = entry.getValue();

                    if (value != null) {

                        pairs.add(new BasicNameValuePair(entry.getKey(), value));

                    }

                }

                // 將請(qǐng)求參數(shù)和url進(jìn)行拼接

                url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));

            }

            HttpGet httpGet = new HttpGet(url);

            CloseableHttpResponse response = httpClient.execute(httpGet);

            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode != 200) {

                httpGet.abort();

                throw new RuntimeException("HttpClient,error status code :" + statusCode);

            }

            HttpEntity entity = response.getEntity();

            String result = null;

            if (entity != null) {

                result = EntityUtils.toString(entity, "utf-8");

            }

            EntityUtils.consume(entity);

            response.close();

            return result;

        } catch (Exception e) {

            log.error("請(qǐng)求服務(wù)器端出錯(cuò):" + e);

            return null;

        }

    }

    /**

     * HTTP Post 獲取內(nèi)容

     *

     * @param url     請(qǐng)求的url地址 ?之前的地址

     * @param params  請(qǐng)求的參數(shù)

     * @param charset 編碼格式

     * @return 頁(yè)面內(nèi)容

     * @throws IOException

     */

    public static String doPost(String url, Map<String, String> params, String charset)

            throws IOException {

        List<NameValuePair> pairs = null;

        if (params != null && !params.isEmpty()) {

            pairs = new ArrayList<NameValuePair>(params.size());

            for (Map.Entry<String, String> entry : params.entrySet()) {

                String value = entry.getValue();

                if (value != null) {

                    pairs.add(new BasicNameValuePair(entry.getKey(), value));

                }

            }

        }

        HttpPost httpPost = new HttpPost(url);

        if (pairs != null && pairs.size() > 0) {

            httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));

        }

        CloseableHttpResponse response = null;

        try {

            response = httpClient.execute(httpPost);

            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode != 200) {

                httpPost.abort();

                throw new RuntimeException("HttpClient,error status code :" + statusCode);

            }

            HttpEntity entity = response.getEntity();

            String result = null;

            if (entity != null) {

                result = EntityUtils.toString(entity, "utf-8");

            }

            EntityUtils.consume(entity);

            return result;

        } catch (ParseException e) {

            log.error("請(qǐng)求服務(wù)器端出錯(cuò):" + e);

            return null;

        } finally {

            if (response != null)

                response.close();

        }

    }

}

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多