package com.lemon.utils;


import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.impl.client.BasicResponseHandler;

/**
 * @program: Lemon
 * @description:
 * @author: ray
 * @create: 2022-11-14 13:54
 **/
public class Robot {

    public static String WechatWebHook() throws Exception {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fc6f4a8e-b208-47ab-9ceb-e9c7e47564c7";
        String json = "{\"msgtype\":\"text\",\"text\":{\"content\":\"msg\",\"mentioned_mobile_list\":[\"17611377226\"]}}";
        // 请求url
        HttpPost httpPost = new HttpPost(url);
        // 请求header
        httpPost.setHeader("Content-Type", "application/json");
        // 添加请求参数
        httpPost.setEntity(new StringEntity(json, "UTF-8"));
        //
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        int statusCode = response.getStatusLine().getStatusCode();
        Header[] headers = response.getAllHeaders();

        String body = EntityUtils.toString(entity);
        return body;
    }


    public static String httpClientPost(String url, String jsonData) {
        String result = "";
        // 创建post方式请求对象
        HttpPost httpPost = new HttpPost(url);
        // 创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            BasicResponseHandler handler = new BasicResponseHandler();
            //设置请求格式
            StringEntity entity = new StringEntity(jsonData, "utf-8"); //解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            //执行POST请求
            result = httpClient.execute(httpPost, handler);
            return result;
        } catch (Exception e) {

        } finally {
            //释放连接
            try {
                httpClient.close();
            } catch (Exception e) {
            }
        }
        return result;
    }


    public static String httpClientGet(String url, String jsonData) {
        String result = "";
        //创建post方式请求对象
        HttpGet httpGet = new HttpGet(url);
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            BasicResponseHandler handler = new BasicResponseHandler();
            //设置请求格式
            StringEntity entity = new StringEntity(jsonData, "utf-8"); //解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            //执行POST请求
            result = httpClient.execute(httpGet, handler);
            return result;
        } catch (Exception e) {

        } finally {
            //释放连接
            try {
                httpClient.close();
            } catch (Exception e) {
            }
        }
        return result;
    }







    public static void main(String[] args) throws Exception {
        // 获取最后一次构建的编号
        String last_build_url = "http://192.168.92.13:9988/job/ryasdata_api_test/lastBuild/buildNumber";
        //
        String url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fc6f4a8e-b208-47ab-9ceb-e9c7e47564c7";
        String json = "{\"msgtype\":\"text\",\"text\":{\"content\":\"msg\",\"mentioned_mobile_list\":[\"17611377226\"]}}";
        String body=WechatWebHook();
        System.out.println(body);
    }


}
