package com.inspect.base.core.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class HttpClientUtils {
    public static String post(String requestUrl) {
        return post(requestUrl, "", "");
    }

    public static String post(String requestUrl, String requestPram) {
        return post(requestUrl, requestPram, "");
    }

    public static String post(String requestUrl, String requestPram, String accessToken) {
        OutputStreamWriter outputStreamWriter = null;
        BufferedReader bufferedReader = null;
        StringBuffer responseResult = new StringBuffer();
        HttpURLConnection httpURLConnection = null;

        try {
            URL realUrl = new URL(requestUrl);
            httpURLConnection = (HttpURLConnection) realUrl.openConnection();
            httpURLConnection.setRequestProperty("accept", "*/*");
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpURLConnection.setRequestProperty("connection", "Keep-Alive");
            if (!accessToken.equals("") || accessToken != null) {
                httpURLConnection.setRequestProperty("access-token", accessToken);
            }

            httpURLConnection.setRequestProperty("Content-Length", String.valueOf(requestPram.length()));
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8");
            outputStreamWriter.write(requestPram);
            outputStreamWriter.flush();
            bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                responseResult.append(line);
            }
        } catch (Exception var17) {
            var17.printStackTrace();
        } finally {
            httpURLConnection.disconnect();

            try {
                if (outputStreamWriter != null) {
                    outputStreamWriter.close();
                }

                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return responseResult.toString();
    }

    public static InputStream getStream(String url, String param) throws Exception {
        param = param.replace("#", "%23");
        URLConnection connection = getConnection(url, param);
        return connection.getInputStream();
    }

    public static URLConnection getConnection(String url, String param) throws Exception {
        String urlNameString = url + "?" + param;
        URL realUrl = new URL(urlNameString);
        URLConnection connection = realUrl.openConnection();
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        connection.connect();
        return connection;
    }

    public static HttpURLConnection getConnection2(String url, String param) throws Exception {
        String urlNameString = url + "?" + param;
        URL realUrl = new URL(urlNameString);
        HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        connection.connect();
        return connection;
    }

    public static String get(String url, String param) throws Exception {
        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try {
            param = param.replace(" ", "%20");
            param = param.replace("#", "%23");
            URL realUrl = new URL(url + "?" + param);
            URLConnection connection = realUrl.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.connect();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (null != in) {
                in.close();
            }
        }

        return result.toString();
    }

    public static String postJson(String requestUrl, String requestPram, String tokenKey, String accessToken) {
        OutputStreamWriter outputStreamWriter = null;
        BufferedReader bufferedReader = null;
        StringBuffer responseResult = new StringBuffer();
        HttpURLConnection httpURLConnection = null;

        try {
            URL realUrl = new URL(requestUrl);
            httpURLConnection = (HttpURLConnection) realUrl.openConnection();
            httpURLConnection.setRequestProperty("accept", "*/*");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.setRequestProperty("connection", "Keep-Alive");
            if (StringUtils.isNotBlank(accessToken)) {
                if (StringUtils.isNotBlank(tokenKey)) {
                    httpURLConnection.setRequestProperty(tokenKey, accessToken);
                } else {
                    httpURLConnection.setRequestProperty("access-token", accessToken);
                }
            }

            httpURLConnection.setRequestProperty("Content-Length", String.valueOf(requestPram.length()));
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8");
            outputStreamWriter.write(requestPram);
            outputStreamWriter.flush();
            bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                responseResult.append(line);
            }
        } catch (Exception var18) {
            var18.printStackTrace();
        } finally {
            httpURLConnection.disconnect();

            try {
                if (outputStreamWriter != null) {
                    outputStreamWriter.close();
                }

                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException var17) {
                var17.printStackTrace();
            }

        }

        return responseResult.toString();
    }

    public static String postJson(String requestUrl, String requestPram, String accessToken) {
        return postJson(requestUrl, requestPram, "", accessToken);
    }

    public static String sendPostAgain(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        URLConnection conn = null;

        try {
            URL realUrl = new URL(url);
            conn = realUrl.openConnection();
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(120000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
            out.print(param);
            out.flush();
            InputStream stream = conn.getInputStream();
            in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));

            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception var19) {
            var19.printStackTrace();

            try {
                in = new BufferedReader(new InputStreamReader(((HttpURLConnection) conn).getErrorStream(), "UTF-8"));

                String line;
                while ((line = in.readLine()) != null) {
                    result.append(line);
                }
            } catch (IOException var18) {
                var18.printStackTrace();
            }
        } finally {
            try {
                if (out != null) {
                    out.close();
                }

                if (in != null) {
                    in.close();
                }
            } catch (IOException var17) {
                var17.printStackTrace();
            }

        }

        return result.toString();
    }
}
