package cn.iocoder.yudao.module.smartfactory.framework.util;


import org.apache.http.HttpResponse;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.*;

/**
 * @author qun.xu
 * @version 1.0.0
 * @date 2019.03.28
 */

public class HttpUtil {
    /**
     * GET请求 带头部信息
     *
     * @param url
     * @param headers
     * @return
     */
    public static String doGetSetHeader(String url, Map<String, String> headers) {
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet request = new HttpGet(url);
            headers.forEach((k, v) -> request.setHeader(k, v));
            HttpResponse response = httpClient.execute(request);
            return EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
