package com.ebts.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.config.RequestConfig;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @Author 18209
 * @Date 2021/6/1 22:43
 * @Email clay@hchyun.com
 * @Version 1.0
 */
public class HttpClient {


    public static String doPost(String url, JSONObject js) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        String jsonString = js + "".toString();
        StringEntity entity = new StringEntity(jsonString, "UTF-8");
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        CloseableHttpResponse response = null;

        String var8;
        try {
            response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity == null) {
                return "erro";
            }

            System.out.println("响应内容长度为:" + responseEntity.getContentLength());
            System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            var8 = EntityUtils.toString(responseEntity);
        } catch (IOException | ParseException var19) {
            var19.printStackTrace();
            var8 = "erro";
            return var8;
        } finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }

                if (response != null) {
                    response.close();
                }
            } catch (IOException var18) {
                var18.printStackTrace();
            }

        }

        return var8;
    }

    public static String doGet(String url) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        StringBuilder params = new StringBuilder();

        try {
            params.append("name=").append(URLEncoder.encode("&", "utf-8")).append("&").append("age=24");
        } catch (UnsupportedEncodingException var20) {
            var20.printStackTrace();
        }

        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;

        String var8;
        try {
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(5000).setRedirectsEnabled(true).build();
            httpGet.setConfig(requestConfig);
            response = httpClient.execute(httpGet);
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity == null) {
                return "erro";
            }

            System.out.println("响应内容长度为:" + responseEntity.getContentLength());
            String rul = EntityUtils.toString(responseEntity);
            if (httpClient != null) {
                httpClient.close();
            }

            if (response != null) {
                response.close();
            }

            var8 = rul;
        } catch (IOException | ParseException var21) {
            var21.printStackTrace();
            return "erro";
        } finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }

                if (response != null) {
                    response.close();
                }
            } catch (IOException var19) {
                var19.printStackTrace();
            }

        }

        return var8;
    }

}
