package com.ruoyi.wms.util;

/**
 * 作者：御坂美琴
 * date: 9/15/2021
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * java发送http的get和post请求
 * */
public class HttpRequest {
    /**
     * ?
     * @param urlPath ?
     * @param jsonStr ?
     * @return ?
     */
    public static String doPost(String urlPath, String jsonStr) {
        String result = "";
        BufferedReader reader = null;

        try {
            URL url = new URL(urlPath);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            //设置请求方式为POST
            conn.setRequestMethod("POST");
            // 设置超时为10秒
            conn.setConnectTimeout(60 * 1000);
            conn.setReadTimeout(60 * 1000);
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //不使用缓存
            conn.setUseCaches(false);
            //使用Keep-Alive连接方式
            conn.setRequestProperty("Connection", "Keep-Alive");
            //设置字符格式为UTF-8
            conn.setRequestProperty("Charset","UTF-8");
            //设置正文格式为json且字符格式为UTF-8
            conn.setRequestProperty("Content-Type","application/json;charset=UTF-8");
            // 设置接收类型否则返回415错误
            //conn.setRequestProperty("accept","*/*")此处为暴力方法设置接受所有类型，以此来防范返回415;
            //设置接收格式为accept
            conn.setRequestProperty("accept","application/json");

            // 往服务器里面发送数据
            byte[] writebytes = jsonStr.getBytes();
            conn.setRequestProperty("Content-Length",String.valueOf(writebytes.length));
            OutputStream outwritestream = conn.getOutputStream();
            outwritestream.write(jsonStr.getBytes());
            outwritestream.flush();
            outwritestream.close();
            System.out.println("conn" + ":" + conn.toString());
            System.out.println("conn.getResponseCode()" + ":" + conn.getResponseCode());
            if (conn.getResponseCode() == 200) {
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                result = reader.readLine();
            }

        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！");
            //e.printStackTrace();
        } finally {
            //使用finally块来关闭输出流、输入流
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("发送 IOException 出现异常！" + "IOException" + ":" + e.getMessage());
                }
            }
        }

        return result;
    }

    /**
     * ?
     * @param urlPath ?
     * @param jsonStr ?
     * @return ?
     */
    public static String doGet(String urlPath, String jsonStr) {
        String result = "";
        BufferedReader reader = null;

        try {
            URL url = new URL(urlPath);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置请求方式为GET
            conn.setRequestMethod("GET");
            // 设置超时为10秒
            conn.setConnectTimeout(10 * 1000);
            conn.setReadTimeout(10 * 1000);
            // 发送POST请求必须设置如下两行
            //conn.setDoOutput(true);
            //conn.setDoInput(true);
            //不使用缓存
            conn.setUseCaches(false);
            //使用Keep-Alive连接方式
            conn.setRequestProperty("Connection", "Keep-Alive");
            //设置字符格式为UTF-8
            conn.setRequestProperty("Charset","UTF-8");
            //设置正文格式为json且字符格式为UTF-8
            conn.setRequestProperty("Content-Type","application/json;charset=UTF-8");
            // 设置接收类型否则返回415错误
            //conn.setRequestProperty("accept","*/*")此处为暴力方法设置接受所有类型，以此来防范返回415;
            //设置接收格式为accept
            conn.setRequestProperty("accept","application/json");

            // 往服务器里面发送数据
            //if ((jsonStr != null && !TextUtils.isEmpty(jsonStr)) || true == true) {
            byte[] writebytes = jsonStr.getBytes();
            conn.setRequestProperty("Content-Length",String.valueOf(writebytes.length));
            //OutputStream outwritestream =conn.getOutputStream();
            //outwritestream.write(jsonStr.getBytes());
            //outwritestream.flush();
            //outwritestream.close();
            //}
            System.out.println("conn" + ":" + conn.toString());
            System.out.println("conn.getResponseCode()" + ":" + conn.getResponseCode());
            if (conn.getResponseCode() == 200) {
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                result = reader.readLine();
            }

        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！");
            //e.printStackTrace();
        } finally {
            //使用finally块来关闭输出流、输入流
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("发送 IOException 出现异常！" + "IOException" + ":" + e.getMessage());
                }
            }
        }

        return result;
    }

}
