package com.xxl.job.executor.utils;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.*;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;

/**
 * @author 宋英豪
 */
@Slf4j
public class HttpUtils {

    //发送响应流方法
    public static void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            fileName = new String(fileName.getBytes(), StandardCharsets.UTF_8);
            //response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setContentType("application/vnd.ms-excel;charset=gb2312");

            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static JSONObject bytedanceGetRequest(String accessToken, String url, JSONObject params) {
        // 构造请求
        HttpEntityEnclosingRequestBase httpEntity = new HttpEntityEnclosingRequestBase() {
            @Override
            public String getMethod() {
                return "GET";
            }
        };
        httpEntity.setHeader("Access-Token", accessToken);
        CloseableHttpResponse response = null;
        CloseableHttpClient client = null;
        try {
            client = HttpClientBuilder.create().build();
            httpEntity.setURI(URI.create(url));
            httpEntity.setEntity(new StringEntity(params.toJSONString(), ContentType.APPLICATION_JSON));
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
            httpEntity.setConfig(requestConfig);
            response = client.execute(httpEntity);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuilder result = new StringBuilder();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result.append(line);
                }
                bufferedReader.close();
                return JSONObject.parseObject(result.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static JSONObject bytedancePostRequest(String accessToken, String url, JSONObject params) {
        // 构造请求
        HttpPost httpEntity = new HttpPost(url);
        httpEntity.setHeader("Access-Token", accessToken);
        CloseableHttpResponse response = null;
        CloseableHttpClient client = null;
        try {
            client = HttpClientBuilder.create().build();
            httpEntity.setEntity(new StringEntity(params.toJSONString(), ContentType.APPLICATION_JSON));
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
            httpEntity.setConfig(requestConfig);
            response = client.execute(httpEntity);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuilder result = new StringBuilder();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result.append(line);
                }
                bufferedReader.close();
                return JSONObject.parseObject(result.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static CookieStore COOKIESTORE = new BasicCookieStore();
    /**
     * setProxy(new HttpHost("106.125.239.179", 4245))
     */
    public static Map<String, Map<String, String>> fontsMap = new HashMap<>();
    public static String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36";

    public static CloseableHttpClient createSslClientDefault() {
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                //信任所有证书
                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();
            RequestConfig globalConfig = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).setCookieSpec(CookieSpecs.STANDARD).build();
            SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext);
            return HttpClients.custom().setDefaultCookieStore(COOKIESTORE).setDefaultRequestConfig(globalConfig).setConnectionReuseStrategy((response, context) -> false).setSSLSocketFactory(sslFactory).build();
        } catch (Exception e) {
            log.error("处理Https证书异常", e);
        }
        return HttpClients.createDefault();
    }

    public static String httpPostFormRequest(String url, Map<String, Object> param, Map<String, String> headers) {
        HttpClient httpClient = createSslClientDefault();
        String strReturn = "";
        try {
            HttpPost httppost = new HttpPost(url);
            if (headers != null) {
                for (String key : headers.keySet()) {
                    httppost.setHeader(key, headers.get(key));
                }
            }
            List<NameValuePair> paramList = new ArrayList<NameValuePair>();
            if (param != null) {
                for (String key : param.keySet()) {
                    BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, String.valueOf(param.get(key)));
                    paramList.add(basicNameValuePair);
                }
                // 第二步：我们发现Entity是一个接口，所以只能找实现类，发现实现类又需要一个集合，集合的泛型是NameValuePair类型
                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramList);
                // 第一步：通过setEntity 将我们的entity对象传递过去
                httppost.setEntity(formEntity);
            }

            HttpEntity respentity;

            HttpResponse response = httpClient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                String newUrl = response.getFirstHeader("Location").getValue();
                return httpPostFormRequest(newUrl, param, headers);
            } else if (statusCode == HttpStatus.SC_OK) {
                respentity = response.getEntity();
                strReturn = EntityUtils.toString(respentity);
                return strReturn;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return strReturn;
    }

    public static String httpPostParamRequest(String url, Map<String, Object> param, Map<String, String> headers) {
        HttpClient httpClient = createSslClientDefault();
        String strReturn = "";
        try {
            HttpPost httppost = new HttpPost(url);
            httppost.setHeader("User-Agent", USER_AGENT);
            if (headers != null) {
                for (String key : headers.keySet()) {
                    httppost.setHeader(key, headers.get(key));
                }
            }
            List<NameValuePair> paramList = new ArrayList<NameValuePair>();
            for (String key : param.keySet()) {
                BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, String.valueOf(param.get(key)));
                paramList.add(basicNameValuePair);
            }

            // 第二步：我们发现Entity是一个接口，所以只能找实现类，发现实现类又需要一个集合，集合的泛型是NameValuePair类型
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramList);
            // 第一步：通过setEntity 将我们的entity对象传递过去
            httppost.setEntity(formEntity);
            httppost.setEntity(new StringEntity(new Gson().toJson(param), Charset.forName("UTF-8")));
            HttpEntity respentity;

            HttpResponse response = httpClient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                String newUrl = response.getFirstHeader("Location").getValue();
                return httpPostParamRequest(newUrl, param, headers);
            } else if (statusCode == HttpStatus.SC_OK) {
                respentity = response.getEntity();
                strReturn = EntityUtils.toString(respentity);
                return strReturn;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return strReturn;
    }

    public static String httpPostRequest(String url, Map<String, Object> param, Map<String, String> headers) {
        HttpClient httpClient = createSslClientDefault();
        String strReturn = "";
        try {
            HttpPost httppost = new HttpPost(url);
            httppost.setHeader("User-Agent", USER_AGENT);
            httppost.addHeader("Content-Type", "application/json");
            if (headers != null && headers.size() > 0) {
                for (String key : headers.keySet()) {
                    httppost.setHeader(key, headers.get(key));
                }
            }
            if (param != null && param.size() > 0) {
                httppost.setEntity(new StringEntity(new Gson().toJson(param), Charset.forName("UTF-8")));
            }
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(300 * 1000).setConnectTimeout(300 * 1000).build();
            httppost.setConfig(requestConfig);
            HttpEntity respentity;

            HttpResponse response = httpClient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                String newUrl = response.getFirstHeader("Location").getValue();
                return httpPostRequest(newUrl, param, headers);
            } else if (statusCode == HttpStatus.SC_OK) {
                respentity = response.getEntity();
                strReturn = EntityUtils.toString(respentity);
                return strReturn;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return strReturn;
    }

    public static String httpPostNoParamRequest(String url) {
        HttpClient httpClient = createSslClientDefault();
        String strReturn = "";
        try {
            HttpPost httppost = new HttpPost(url);
            httppost.setHeader("User-Agent", USER_AGENT);
            httppost.addHeader("Content-Type", "application/json");
            HttpEntity respentity;
            HttpResponse response = httpClient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                String newUrl = response.getFirstHeader("Location").getValue();
                return httpPostNoParamRequest(newUrl);
            } else if (statusCode == HttpStatus.SC_OK) {
                respentity = response.getEntity();
                strReturn = EntityUtils.toString(respentity);
                return strReturn;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return strReturn;
    }


    public static String httpPostRequestTest(String url, String body, Map<String, String> headers) {
        HttpClient httpClient = createSslClientDefault();
        String strReturn = "";
        try {
            HttpPost httppost = new HttpPost(url);
            httppost.setHeader("User-Agent", USER_AGENT);
            httppost.addHeader("Content-Type", "application/json");
            if (!Check.isNull(headers)) {
                for (String key : headers.keySet()) {
                    httppost.setHeader(key, headers.get(key));
                }
            }

            httppost.setEntity(new StringEntity(body, Charset.forName("UTF-8")));
            HttpEntity respentity;

            HttpResponse response = httpClient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                String newUrl = response.getFirstHeader("Location").getValue();
                return httpPostRequestTest(newUrl, body, headers);
            } else if (statusCode == HttpStatus.SC_OK) {
                respentity = response.getEntity();
                strReturn = EntityUtils.toString(respentity);
                return strReturn;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return strReturn;
    }


    public static String kuaiShouhttpPostRequest(String url, String body, Map<String, String> headers) {
        HttpClient httpClient = createSslClientDefault();
        String strReturn = "";
        try {
            HttpPost httppost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(120000).setConnectTimeout(120000).build();
            httppost.setConfig(requestConfig);
            if (!Check.isNullMap(headers)) {
                for (String key : headers.keySet()) {
                    httppost.setHeader(key, headers.get(key));
                }
            }

            httppost.setHeader("User-Agent", USER_AGENT);
            if (!Check.isNull(body)) {
                httppost.setEntity(new StringEntity(body, Charset.forName("UTF-8")));
            }
            HttpEntity respentity;
            HttpResponse response = httpClient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                respentity = response.getEntity();
                strReturn = EntityUtils.toString(respentity);
                return strReturn;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return strReturn;
    }


    public static String httpPostRequest(String url, JSONObject params, Map<String, String> headers) {
        HttpClient httpClient = createSslClientDefault();
        String strReturn = "";
        try {
            HttpPost httppost = new HttpPost(url);
            httppost.setHeader("User-Agent", USER_AGENT);
            httppost.addHeader("Content-Type", "application/json");
            for (String key : headers.keySet()) {
                httppost.setHeader(key, headers.get(key));
            }
            httppost.setEntity(new StringEntity(params.toJSONString(), Charset.forName("UTF-8")));
            HttpEntity respentity;

            HttpResponse response = httpClient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                String newUrl = response.getFirstHeader("Location").getValue();
                return httpPostRequest(newUrl, params, headers);
            } else if (statusCode == HttpStatus.SC_OK) {
                respentity = response.getEntity();
                strReturn = EntityUtils.toString(respentity);
                return strReturn;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return strReturn;
    }

    public static void main(String[] args) {
        String res = HttpUtils.getKuaishouShareUrl("http://yongzhou.s.gifshow.com/i/photo/lwx?userId=1400097524&photoId=5191805968879099278");
        String path = res.split("\\?")[0];
        String photoId = path.substring(path.lastIndexOf("/") + 1, path.length());
        path = path.substring(0, path.lastIndexOf("/"));
        String uid = path.substring(path.lastIndexOf("/") + 1, path.length());

        res = HttpUtils.postKuaishouVideoUrl("https://live.kuaishou.com/m_graphql", uid, photoId);
    }

    public static String postKuaishouVideoUrl(String url, String uid, String photoId) {
        HttpClient httpClient = createSslClientDefault();
        String strReturn = "";
        String paramBody = "{\"operationName\":\"FeedQuery\",\"variables\":{\"principalId\":\"" + uid + "\",\"photoId\":\"" + photoId + "\"},\"query\":\"query FeedQuery($principalId: String, $photoId: String) {\\n  feedById(principalId: $principalId, photoId: $photoId) {\\n    currentWork {\\n                 timestamp\\n           __typename\\n    }\\n       __typename\\n  }\\n}\\n\"}";
        try {
            HttpPost httppost = new HttpPost(url);
            httppost.setHeader("User-Agent", USER_AGENT);
            httppost.addHeader("Content-Type", "application/json");
//            for (String key : headers.keySet()) {
//                httppost.setHeader(key, headers.get(key));
//            }
            httppost.setEntity(new StringEntity(paramBody, Charset.forName("UTF-8")));
            HttpEntity respentity;

            HttpResponse response = httpClient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();

            respentity = response.getEntity();
            strReturn = EntityUtils.toString(respentity);
            return strReturn;

        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return strReturn;
    }

    public static String getKuaishouShareUrl(String url) {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpResponse response = null;
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build());
        String result = null;
        try {
            httpGet.setHeader("User-Agent", USER_AGENT);
            httpGet.setHeader("Accept-Encoding", "gzip, deflate, br");
            httpGet.setHeader("Connection", "keep-alive");
            httpGet.setHeader("Host", "yongzhou.s.gifshow.com");
            httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
            httpGet.setHeader("Upgrade-Insecure-Requests", "1");
            httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                String newUrl = response.getFirstHeader("Location").getValue();
                return newUrl;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String httpGetRequest(String url) {
        HttpClient httpClient = createSslClientDefault();
        HttpResponse response = null;
        HttpGet httpGet = new HttpGet(url);
        String result = null;
        try {
            httpGet.setHeader("User-Agent", USER_AGENT);
            response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                String newUrl = response.getFirstHeader("Location").getValue();
                return httpGetRequest(newUrl);
            } else if (statusCode == HttpStatus.SC_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String line = null;
                StringBuilder builder = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
                result = builder.toString();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String httpRequest(String url, String strParams) throws Exception {
        HttpClient httpClient = createSslClientDefault();
        String strReturn = "";
        try {

            HttpPost httppost = new HttpPost(url);
            httppost.addHeader("Content-Type", "application/json");
            httppost.setEntity(new StringEntity(strParams, Charset.forName("UTF-8")));
            HttpEntity respentity;
            HttpResponse response = httpClient.execute(httppost);
            respentity = response.getEntity();
            strReturn = EntityUtils.toString(respentity);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return strReturn;
    }

    public static String httpRequest(String url, CookieStore cookies, Map<String, String> parameterMap) throws Exception {
        HttpClient httpClient = createSslClientDefault();
        String strReturn = "";
        try {
            HttpPost httppost = new HttpPost(url);
            httppost.setHeader("Content-Type", "application/json");
            UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(
                    getParam(parameterMap), "UTF-8");
            httppost.setEntity(postEntity);
            HttpEntity respentity;
            HttpResponse response = httpClient.execute(httppost);
            respentity = response.getEntity();
            strReturn = EntityUtils.toString(respentity);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return strReturn;
    }

    public static List<NameValuePair> getParam(Map parameterMap) {
        List<NameValuePair> param = new ArrayList<>();
        Iterator it = parameterMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry parmEntry = (Map.Entry) it.next();
            param.add(new BasicNameValuePair((String) parmEntry.getKey(),
                    (String) parmEntry.getValue()));
        }
        return param;
    }

    public static HttpClient getHttpclient() {
        RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(180000).setConnectTimeout(30000)
                .setConnectionRequestTimeout(30000).setStaleConnectionCheckEnabled(true).build();
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
        return httpclient;
    }

    public static Map<String, Integer> KUAISHOUNUMBERMAP;

    public static String mapParamsSortToStringBySeperator(TreeMap<String, Object> treeMap, String seperator) {
        String result = "";
        if (null != treeMap && !treeMap.isEmpty()) {
            for (Map.Entry<String, Object> entry : treeMap.entrySet()) {
                String key = entry.getKey();
                String value = URLEncoder.encode((String) entry.getValue().toString());
                result += (key + "=" + value + seperator);
            }
            if (result.length() > 0) {
                result = result.substring(0, result.length() - seperator.length());
            }
        }
        return result;
    }

    public static String httpGetRequest(String url, Map<String, String> headers, TreeMap<String, Object> params) {
        HttpClient httpClient = createSslClientDefault();
        String strReturn = "";
        try {
            String uri = url + "?" + mapParamsSortToStringBySeperator(params, "&");
            System.err.println(uri);
            HttpGet httpGet = new HttpGet(uri);
            if (headers != null) {
                Iterator<String> keyIter = headers.keySet().iterator();
                while (keyIter.hasNext()) {
                    String curKey = keyIter.next();
                    if (curKey != null && headers.get(curKey) != null) {
                        httpGet.addHeader(curKey, headers.get(curKey));
                    }
                }
            }
            HttpEntity respentity;
            HttpResponse response = httpClient.execute(httpGet);
            respentity = response.getEntity();
            strReturn = EntityUtils.toString(respentity);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return strReturn;
    }

    public static String httpGetRequest(String url, Map<String, String> headers, JSONObject params) {
        HttpClient httpClient = createSslClientDefault();
        String strReturn = "";
        try {
            HttpGetWithBodyEntity httpGet = new HttpGetWithBodyEntity(url);
            if (headers != null) {
                Iterator<String> keyIter = headers.keySet().iterator();
                while (keyIter.hasNext()) {
                    String curKey = keyIter.next();
                    if (curKey != null && headers.get(curKey) != null) {
                        httpGet.addHeader(curKey, headers.get(curKey));
                    }
                }
            }
            httpGet.setEntity(new StringEntity(params.toJSONString(), Charset.forName("UTF-8")));
            HttpEntity respentity;
            HttpResponse response = httpClient.execute(httpGet);
            respentity = response.getEntity();
            strReturn = EntityUtils.toString(respentity);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return strReturn;
    }


    public static String httpGet(String url, Map<String, Object> paramsMap, Map<String, String> headers) {
        String result = null;
        try {
            //创建参数列表
            HttpClient httpclient = getHttpclient();
            StringBuilder postBody = null;
            if (!Check.isNull(paramsMap)) {
                postBody = new StringBuilder();
                for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
                    if (entry.getValue() == null) {
                        continue;
                    }
                    postBody.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue().toString(),
                            "utf-8")).append("&");
                }
                if (!paramsMap.isEmpty()) {
                    postBody.deleteCharAt(postBody.length() - 1);
                }
            }
            String urlStr;
            if (!Check.isNull(postBody)) {
                urlStr = url + "?" + postBody;
            } else {
                urlStr = url;
            }
            HttpGet httpget = new HttpGet(urlStr);
            if (!Check.isNullMap(headers)) {
                for (String key : headers.keySet()) {
                    httpget.setHeader(key, headers.get(key));
                }
            }
            HttpEntity respEntity;
            HttpResponse response = httpclient.execute(httpget);
            respEntity = response.getEntity();
            Header[] responseHeaders = response.getHeaders("Set-Cookie");
            if (responseHeaders != null) {
                for (Header header : responseHeaders) {
                    String[] ckArray = header.getValue().split(";");
                    BasicClientCookie ck = new BasicClientCookie(ckArray[0].split("=")[0], ckArray[0].split("=")[1]);
                    ck.setPath(ckArray[1].split("=")[1]);
                    ck.setDomain(ckArray[2].split("=")[1]);
                    HttpUtils.COOKIESTORE.addCookie(ck);
                }
            }
            result = EntityUtils.toString(respEntity);
        } catch (Exception e) {
            log.error("http get请求异常");
            e.printStackTrace();
        }
        return result;
    }


    public static JSONObject kuaiShouHttpGetRequest(String accessToken, String url, JSONObject params) {
        // 构造请求
        HttpEntityEnclosingRequestBase httpEntity = new HttpEntityEnclosingRequestBase() {
            @Override
            public String getMethod() {
                return "GET";
            }
        };
        httpEntity.setHeader("Access-Token", accessToken);
        CloseableHttpResponse response = null;
        CloseableHttpClient client = null;
        try {
            client = HttpClientBuilder.create().build();
            httpEntity.setURI(URI.create(url));
            httpEntity.setEntity(new StringEntity(params.toJSONString(), ContentType.APPLICATION_JSON));
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
            httpEntity.setConfig(requestConfig);
            response = client.execute(httpEntity);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuilder result = new StringBuilder();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result.append(line);
                }
                bufferedReader.close();
                return JSONObject.parseObject(result.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public static String KuaiShouttpGetRequest(String url, Map<String, Object> params, Map<String, String> headers) throws Exception {
        HttpClient httpclient = getHttpclient();
        String strReturn = "";
        try {


            StringBuilder postBody = new StringBuilder();
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                if (entry.getValue() == null) {
                    continue;
                }
                postBody.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue().toString(),
                        "utf-8")).append("&");
            }
            if (!params.isEmpty()) {
                postBody.deleteCharAt(postBody.length() - 1);
            }

            HttpGet httpget = new HttpGet(url + "?" + postBody);
            if (headers != null) {
                Iterator<String> keyIter = headers.keySet().iterator();
                while (keyIter.hasNext()) {
                    String curKey = keyIter.next();
                    if (curKey != null && headers.get(curKey) != null) {
                        httpget.addHeader(curKey, headers.get(curKey));
                    }
                }
            }
            HttpEntity respentity;
            HttpResponse response = httpclient.execute(httpget);
            respentity = response.getEntity();
            strReturn = EntityUtils.toString(respentity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strReturn;
    }

    public static JSONObject feishuGetRequest(String url, JSONObject params) {
        return feishuGetRequest(null, url, params);
    }

    public static JSONObject feishuPostRequest(String url, JSONObject params) {
        return feishuPostRequest(null, url, params);
    }

    public static String feishuGetRequestString(String url) {
        // 构造请求
        HttpEntityEnclosingRequestBase httpEntity = new HttpEntityEnclosingRequestBase() {
            @Override
            public String getMethod() {
                return "GET";
            }
        };
        CloseableHttpResponse response = null;
        CloseableHttpClient client = null;
        try {
            client = HttpClientBuilder.create().build();
            httpEntity.setURI(URI.create(url));
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
            httpEntity.setConfig(requestConfig);
            response = client.execute(httpEntity);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuilder result = new StringBuilder();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result.append(line);
                }
                bufferedReader.close();
                return result.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static JSONObject feishuGetRequest(String accessToken, String url, JSONObject params) {
        // 构造请求
        HttpEntityEnclosingRequestBase httpEntity = new HttpEntityEnclosingRequestBase() {
            @Override
            public String getMethod() {
                return "GET";
            }
        };
        if (accessToken != null) {
            httpEntity.setHeader("Authorization", accessToken);
        }
        CloseableHttpResponse response = null;
        CloseableHttpClient client = null;
        try {
            client = HttpClientBuilder.create().build();
            httpEntity.setURI(URI.create(url));
            if (params != null) {
                httpEntity.setEntity(new StringEntity(params.toJSONString(), ContentType.APPLICATION_JSON));
            }
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
            httpEntity.setConfig(requestConfig);
            response = client.execute(httpEntity);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuilder result = new StringBuilder();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result.append(line);
                }
                bufferedReader.close();
                return JSONObject.parseObject(result.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public static JSONObject feishuPostRequest(String accessToken, String url, JSONObject params) {
        // 构造请求
        HttpPost httpEntity = new HttpPost(url);
        if (accessToken != null) {
            httpEntity.setHeader("Authorization", accessToken);
        }
        CloseableHttpResponse response = null;
        CloseableHttpClient client = null;
        try {
            client = HttpClientBuilder.create().build();
            httpEntity.setEntity(new StringEntity(params.toJSONString(), ContentType.APPLICATION_JSON));
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
            httpEntity.setConfig(requestConfig);
            response = client.execute(httpEntity);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuilder result = new StringBuilder();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result.append(line);
                }
                bufferedReader.close();
                return JSONObject.parseObject(result.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
