HttpClientUtil的一个实例

2021-01-27  本文已影响0人  King斌
package com.xxxxx.util.http;

import java.io.IOException;
import java.net.URI;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
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.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


public class HttpClientUtil {

    /***
     *
     * @status done
     * @Author wangb
     * @param url 路径
     * @Param param 参数
     * @Param header 请求头
     * @return String
     */
    public static String doGet(String url, Map<String, String> param, Map<String, String> header) {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (null != param) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.build();
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            // 从传参header中获取请求头
            if (null != header) {
                header.forEach((key, value) -> {
                    httpGet.setHeader(key, value);
                });
            }
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }

    /***
     *
     * @status done
     * @Author wangb
     * @param url 请求路径
     * @return String
     */
    public static String doGet(String url) {
        return doGet(url, null, null);
    }

    /***
     *
     * @status done
     * @Author wangb
     * @param url 路径
     * @Param param 参数
     * @return String
     */
    public static String doGet(String url, Map<String, String> param) {
        return doGet(url, param, null);
    }

    /***
     *
     * @status done
     * @Author wangb
     * @param url 路径
     * @Param param 参数
     * @Param body 表单
     * @Param header 请求头
     * @return String
     */
    public static String doPost(String url, Map<String, String> param, JSONObject body, Map<String, String> header) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (null != param) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.build();
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(uri);
            // 从传参header中获取请求头
            if (null != header) {
                header.forEach((key, value) -> {
                    httpPost.setHeader(key, value);
                });
            }
            // 创建参数列表
            if (null != body) {
                // 模拟表单
                StringEntity entity = new StringEntity(body.toJSONString());
                entity.setContentEncoding("UTF-8");
                // 设置表单的格式为json
                entity.setContentType("application/json");
                httpPost.setEntity(entity);
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return resultString;
    }

    /***
     *
     * @status done
     * @Author wangb
     * @param url 路径
     * @return String
     */
    public static String doPost(String url) {
        return doPost(url, null, null, null);
    }

    /***
     *
     * @status done
     * @Author wangb
     * @param url 路径
     * @Param param 参数
     * @return String
     */
    public static String doPost(String url, Map<String, String> param) {
        return doPost(url, param, null, null);
    }

    /***
     *
     * @status done
     * @Author wangb
     * @param url 路径
     * @Param body 表单
     * @return String
     */
    public static String doPost(String url, JSONObject body) {
        return doPost(url, null, body, null);
    }

    /***
     *
     * @status done
     * @Author wangb
     * @param url 路径
     * @Param param 参数
     * @Param header 请起头
     * @return String
     */
    public static String doPostParam(String url, Map<String, String> param, Map<String, String> header) {
        return doPost(url, param, null, header);
    }

    /***
     *
     * @status done
     * @Author wangb
     * @param url 路径
     * @Param body 表单
     * @Param header 请求头
     * @return String
     */
    public static String doPost(String url, JSONObject body, Map<String, String> header) {
        return doPost(url, null, body, header);
    }


    /***
     *
     * @status done
     * @Author wangb
     * @param url 路径
     * @Param param 参数
     * @Param body 表单
     * @return String
     */
    public static String doPost(String url, Map<String, String> param, JSONObject body) {
        return doPost(url, param, body, null);
    }
 
}

上一篇 下一篇

猜你喜欢

热点阅读