//package cn.estsh.i3plus.pojo.bsp.common;
//
//import cn.estsh.i3plus.pojo.base.bean.BaseResultBean;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import javax.net.ssl.*;
//import java.io.BufferedReader;
//import java.io.DataOutputStream;
//import java.io.InputStreamReader;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.security.cert.CertificateException;
//import java.security.cert.X509Certificate;
//
///**
// * @Description :
// * @Reference :
// * @Author : Castle
// * @CreateDate : 2021/6/17 13:37
// * @Modify:
// **/
//public class HttpUtils {
//    private static Logger logger = LoggerFactory.getLogger(HttpUtils.class);
//
//    // trust-https start
//    private static void trustAllHosts(HttpsURLConnection connection) {
//        try {
//            SSLContext sc = SSLContext.getInstance("TLS");
//            sc.init(null, trustAllCerts, new java.security.SecureRandom());
//            SSLSocketFactory newFactory = sc.getSocketFactory();
//
//            connection.setSSLSocketFactory(newFactory);
//        } catch (Exception e) {
//            logger.error(e.getMessage(), e);
//        }
//        connection.setHostnameVerifier(new HostnameVerifier() {
//            @Override
//            public boolean verify(String hostname, SSLSession session) {
//                return true;
//            }
//        });
//    }
//
//    private static final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
//        @Override
//        public X509Certificate[] getAcceptedIssuers() {
//            return new X509Certificate[]{};
//        }
//
//        @Override
//        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//        }
//
//        @Override
//        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//        }
//    }};
//    // trust-https end
//
//
//    /**
//     * post
//     *
//     * @param url
//     * @param timeout
//     * @param requestObj
//     * @return
//     */
//    public static ResultBean postBody(String url, int timeout, Object requestObj) {
//        HttpURLConnection connection = null;
//        BufferedReader bufferedReader = null;
//        try {
//            // connection
//            URL realUrl = new URL(url);
//            connection = (HttpURLConnection) realUrl.openConnection();
//
//            // trust-https
//            boolean useHttps = url.startsWith("https");
//            if (useHttps) {
//                HttpsURLConnection https = (HttpsURLConnection) connection;
//                trustAllHosts(https);
//            }
//
//            // connection setting
//            connection.setRequestMethod("POST");
//            connection.setDoOutput(true);
//            connection.setDoInput(true);
//            connection.setUseCaches(false);
//            connection.setReadTimeout(timeout * 1000);
//            connection.setConnectTimeout(3 * 1000);
//            connection.setRequestProperty("connection", "Keep-Alive");
//            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
//            connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");
//
//            // do connection
//            connection.connect();
//
//            // write requestBody
//            if (requestObj != null) {
//                String requestBody = GsonTool.toJson(requestObj);
//
//                DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
//                dataOutputStream.write(requestBody.getBytes("UTF-8"));
//                dataOutputStream.flush();
//                dataOutputStream.close();
//            }
//
//            // valid StatusCode
//            int statusCode = connection.getResponseCode();
//            if (statusCode != 200) {
//                return ResultBean.fail("rpc remoting fail, StatusCode(" + statusCode + ") invalid. for url : " + url);
////                return new BaseResultBean().errorMsg("rpc remoting fail, StatusCode(" + statusCode + ") invalid. for url : " + url);
////                return new ReturnT<String>(ReturnT.FAIL_CODE, "rpc remoting fail, StatusCode(" + statusCode + ") invalid. for url : " + url);
//            }
//
//            // result
//            bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
//            StringBuilder result = new StringBuilder();
//            String line;
//            while ((line = bufferedReader.readLine()) != null) {
//                result.append(line);
//            }
//            String resultJson = result.toString();
//            // parse returnT
//            try {
//                return GsonTool.fromJson(resultJson, BaseResultBean.class);
//            } catch (Exception e) {
//                logger.error("rpc remoting (url=" + url + ") response content invalid(" + resultJson + ").", e);
//                return ResultBean.fail("rpc remoting fail, StatusCode(" + statusCode + ") invalid. for url : " + url);
//            }
//        } catch (Exception e) {
//            logger.error(e.getMessage(), e);
//            return ResultBean.fail("rpc remoting fail, error message(" + e.getMessage() + ") invalid. for url : " + url);
//        } finally {
//            try {
//                if (bufferedReader != null) {
//                    bufferedReader.close();
//                }
//                if (connection != null) {
//                    connection.disconnect();
//                }
//            } catch (Exception e2) {
//                logger.error(e2.getMessage(), e2);
//            }
//        }
//    }
//
//}