package com.youchain.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.youchain.exception.BadRequestException;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.http.HttpStatus;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class HttpPostUtil {
    public static String sendPostReq(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            // 设置通用的请求属性
//            conn.setRequestProperty("accept", "*/*");
//            conn.setRequestProperty("connection", "Keep-Alive");
//            conn.setRequestProperty("user-agent",
//                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            conn.setRequestMethod("POST");//默认get

            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //post请求不能使用缓存
            conn.setUseCaches(false);

            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("code","400");
            jsonObject.put("message","接口调用异常");
            result=jsonObject.toString();
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }
    /*public static String sendPostReq(String api_url, String request){
        System.out.println("接口任务下发:"+api_url+":"+request);
        InputStream instr = null;
        String str = "";
        try {
            URL url = new URL(api_url);
            URLConnection urlCon = url.openConnection();
            urlCon.setConnectTimeout(3000);
            byte[] xmlData = request.getBytes();
            urlCon.setDoOutput(true);
            urlCon.setDoInput(true);
            urlCon.setUseCaches(false);
            urlCon.setRequestProperty("content-Type", "application/json");
            urlCon.setRequestProperty("charset", "UTF-8");
            urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length));
            DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream());
            printout.write(xmlData);
            printout.flush();
            printout.close();
            instr = urlCon.getInputStream();
            byte[] bis = IOUtils.toByteArray(instr);
            String ResponseString = new String(bis, "UTF-8");
            if ((ResponseString == null) || ("".equals(ResponseString.trim()))) {
                System.out.println("返回空");
            }
            str = ResponseString;
        }catch (Exception e){
            throw new BadRequestException(HttpStatus.NOT_IMPLEMENTED, "接口调用异常"+api_url+request);
        }
        System.out.println("接口任务返回:"+str);
        return str;
    }*/
}
