/*
 * @Author: Kane
 * @Date: 2022-04-18 11:37:33
 * @LastEditors: Kane
 * @LastEditTime: 2022-04-18 15:58:34
 * @FilePath: \undefinedd:\develop\产险厦门分公司项目\天气灾害预警\文档\example.java

import org.apache.httpcomponents.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

public class HttpUtils {
    public static String sendPostWithJson(String url, String jsonStr, HashMap<String, String> headers) {
        // 返回的结果
        String jsonResult = "";

        try {
            HttpClient client = new HttpClient();

            // 连接超时
            client.getHttpConnectionManager().getParams().setConnectionTimeout(3 * 1000);
            // 读取数据超时
            client.getHttpConnectionManager().getParams().setSoTimeout(3 * 60 * 1000);
            client.getParams().setContentCharset("UTF-8");

            PostMethod postMethod = new PostMethod(url);

            postMethod.setRequestHeader("content-type", headers.get("content-type"));

            // 非空
            if (null != jsonStr && !"".equals(jsonStr)) {
                StringRequestEntity requestEntity = new StringRequestEntity(jsonStr, headers.get("content-type"),
                        "UTF-8");
                postMethod.setRequestEntity(requestEntity);
            }

            int status = client.executeMethod(postMethod);

            if (status == HttpStatus.SC_OK) {
                jsonResult = postMethod.getResponseBodyAsString();
            } else {
                throw new RuntimeException("接口连接失败！");
            }

        } catch (Exception e) {
            throw new RuntimeException("接口连接失败！");
        }
        return jsonResult;
    }

    public static void main(String[] args) {
        String requestUrl = "http://localhost:8070/test/rz/server/rzxx/at_VaildToken.do";
        String jsonStr = "{\"name\":\"张三\"}";

        HashMap<String, String> headers = new HashMap<>(3);
        headers.put("content-type", "application/json");

        // 发送post请求
        String resultData = HttpUtils.sendPostWithJson(requestUrl, jsonStr, headers);

        // 并接收返回结果
        System.out.println(resultData);
    }

}

    /**
     * 
     * @author : cjd
     * 
     * @description : post接口 返回结果字符串
     * 
     * @params : [url, param]
     * 
     * @param url   请求接口
     * 
     * @param param 需要的json字符串
     * 
     * @return :java.lang.String
     * 
     * @date : 17:31 2018/8/1
     * 
     */

    public static String sendPost(String url, String param) 
    {
        OutputStreamWriter out = null;
        BufferedReader in = null;

        String result = "";

        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = null; // 打开和URL之间的连接

            conn = (HttpURLConnection) realUrl.openConnection(); // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST"); // POST方法

            // 设置通用的请求属性
            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.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.connect(); // 获取URLConnection对象对应的输出流

            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); // 发送请求参数
            out.write(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) 
        {
            System.out.println("发送 POST 请求出现异常！" + e);

            e.printStackTrace();

        } // 使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null)
                {
                    out.close();
                }

                if (in != null) 
                {
                    in.close();
                }
            } 
            catch (IOException ex) 
            {
                ex.printStackTrace();
            }
        }
        return result;

}


public String postMethod(String url,String param){
    // 结果值
    StringBuffer rest=new StringBuffer();

    HttpURLConnection conn=null;
    OutputStream out=null;
    BufferedReader br=null;

    try {
        // 创建 URL
        URL restUrl = new URL(url);
        // 打开连接
        conn= (HttpURLConnection) restUrl.openConnection();
        // 设置请求方式为 POST
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection","keep-Alive");
        // 设置接收文件类型
//            conn.setRequestProperty("Accept","application/json");
        //设置发送文件类型
        /**
         这里注意  传递JSON数据的话 就要设置
         普通参数的话 就要注释掉
         */
        conn.setRequestProperty("Content-Type","application/json");
        // 输入 输出 都打开
        conn.setDoOutput(true);
        conn.setDoInput(true);
        //开始连接
        conn.connect();

        // 传递参数  流的方式
        out=conn.getOutputStream();
        out.write(param.getBytes());
        out.flush();

        // 读取数据
        br=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
        String line=null;
        while (null != (line=br.readLine())){
            rest.append(line);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        // 关闭所有通道
        try {
            if (br!=null) {
                br.close();
            }
            if (out!=null) {
                out.close();
            }
            if (conn!=null) {
                conn.disconnect();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
    return rest.toString();
}
