package com.zhgd.xmgl.util;

import lombok.experimental.UtilityClass;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
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;

@UtilityClass
public class HttpUtil {

    public String doPost(String requestUrl, String jsonObj) throws Exception {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(requestUrl);
        //  设置超时时间
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(400000).setConnectTimeout(400000).setConnectionRequestTimeout(400000).build();
        // 设置http请求的请求体信息
        StringEntity entity = new StringEntity(jsonObj, "UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        CloseableHttpResponse execute = client.execute(httpPost);
        if (execute.getStatusLine().getStatusCode() == 200) {
            HttpEntity result = execute.getEntity();
            String respContent = EntityUtils.toString(result,"UTF-8");
            return respContent;
        }
        return null;
    }
}
