package com.tofly.dzfp.utils;

import com.tofly.common.core.util.StringUtil;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpRequestUtil {


    public static String GETRequest(String urlstr, String token) {
        return GETRequest(urlstr, token, "");
    }

    public static String GETRequest(String urlstr, String token, String params) {
        try {
            URL url = null;
            if (!StringUtil.isEmpty(params)) {
                url = new URL(urlstr + "?param=" + params);
            } else {
                url = new URL(urlstr);
            }
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            // 设置头信息
            con.setRequestMethod("GET");
            con.setRequestProperty("X-Access-Token", token);
            con.setRequestProperty("Accept", "application/json, text/plain, */*");
            con.setRequestProperty("X-Sign", "239D943E6EF1E33BDC18BDED629CBE55");
            con.setRequestProperty("X-Version", "v3");

            // 发送请求
            con.connect();

            // 读取响应
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer content = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine.replace("\"", ""));
            }
            in.close();
            if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                con.disconnect();
                return content.toString();
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String POSTRequest(String urlstr, String params) {
        return POSTRequest(urlstr, params, "");
    }

    public static String POSTRequest(String urlstr, String params, String token) {
        try {
            URL url = new URL(urlstr);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            // 设置头信息
            con.setRequestMethod("POST");
            if (StringUtil.isEmpty(token)) {
                con.setRequestProperty("X-Access-Token", "null");
            } else {
                con.setRequestProperty("X-Access-Token", token);
            }

            con.setRequestProperty("Accept", "application/json, text/plain, */*");
            con.setRequestProperty("Content-Type", "application/json");
            //con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            con.setRequestProperty("X-Sign", "239D943E6EF1E33BDC18BDED629CBE55");
            con.setRequestProperty("X-Version", "v3");

            // 发送请求
            con.setDoOutput(true);
            DataOutputStream out = new DataOutputStream(con.getOutputStream());
            out.writeBytes(params);
            out.flush();
            out.close();

            // 读取响应
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer content = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine.replace("\"", ""));
            }
            in.close();

            // 获取身份标识
            if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                con.disconnect();
                return content.toString();
            }
            return "";

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


    public static String POSTFileRequest(String urlstr, String params, String token) {
        try {
            URL url = new URL(urlstr);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            // 设置头信息
            con.setRequestMethod("POST");
            if (StringUtil.isEmpty(token)) {
                con.setRequestProperty("X-Access-Token", "null");
            } else {
                con.setRequestProperty("X-Access-Token", token);
            }

            con.setRequestProperty("Accept", "multipart/form-data,text/plain, */*");
            con.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
            con.setRequestProperty("X-Sign", "239D943E6EF1E33BDC18BDED629CBE55");
            con.setRequestProperty("X-Version", "v3");

            // 发送请求
            con.setDoOutput(true);
            DataOutputStream out = new DataOutputStream(con.getOutputStream());
            out.writeBytes(params);
            out.flush();
            out.close();

            // 读取响应
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer content = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine.replace("\"", ""));
            }
            in.close();

            // 获取身份标识
            if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                con.disconnect();
                return content.toString();
            }
            return "";

        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    public static String PUTRequest(String urlstr, String params, String token) {
        try {
            URL url = new URL(urlstr);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            // 设置头信息
            con.setRequestMethod("PUT");
            if (StringUtil.isEmpty(token)) {
                con.setRequestProperty("X-Access-Token", "null");
            } else {
                con.setRequestProperty("X-Access-Token", token);
            }

            con.setRequestProperty("Accept", "application/json, text/plain, */*");
            con.setRequestProperty("Content-Type", "application/json");
            //con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            con.setRequestProperty("X-Sign", "239D943E6EF1E33BDC18BDED629CBE55");
            con.setRequestProperty("X-Version", "v3");

            // 发送请求
            con.setDoOutput(true);
            DataOutputStream out = new DataOutputStream(con.getOutputStream());
            out.writeBytes(params);
            out.flush();
            out.close();

            // 读取响应
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer content = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine.replace("\"", ""));
            }
            in.close();

            // 获取身份标识
            if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                con.disconnect();
                return content.toString();
            }
            return "";

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