package com.ruoyi.system.yyinterface;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * 2024/5/27
 */
public class HttpUtil {

    public static String getDomain(HttpServletRequest request) {
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
        return tempContextUrl;
    }

    public static void sendResponse(HttpServletResponse response, String str) {
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.print(str);
            writer.close();
            response.flushBuffer();
        } catch (IOException e) {
            if (writer != null) {
                writer.close();
            }
        }
    }

    public static ResultVO doPost(String url, List<NameValuePair> params, Map<String, String> headers) throws Exception {
        ResultVO resultVO = new ResultVO();
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{
            httpClient = HttpClients.createDefault();
            httpPost = new HttpPost(url);
            if(params!=null&&params.size()>0) {
                //添加参数
                httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            }
            if(headers!=null&&headers.size()>0) {
                for(Map.Entry<String, String> entry : headers.entrySet()) {
                    httpPost.addHeader(entry.getKey(), entry.getValue());
                }
            }
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){
                int status = response.getStatusLine().getStatusCode();
                if(status >= 200 && status<300) {
                    HttpEntity resEntity = response.getEntity();
                    if(resEntity != null){
                        result = EntityUtils.toString(resEntity, "utf-8");
                    }
                    resultVO.setData(result);
                    resultVO.setSuccess(true);
                } else {
                    resultVO.setMessage(String.valueOf(status));
                }
            }
        }catch(Exception ex){
            // ex.printStackTrace();
            throw ex;
        }
        return resultVO;
    }

    public static ResultVO doPost(String url, String requestBody, Map<String, String> headers) throws Exception {
        ResultVO resultVO = new ResultVO();
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{
            httpClient = HttpClients.createDefault();
            httpPost = new HttpPost(url);
            if(requestBody!=null&&requestBody.length()>0) {
                //添加参数
                httpPost.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
            }
            if(headers!=null&&headers.size()>0) {
                for(Map.Entry<String, String> entry : headers.entrySet()) {
                    httpPost.addHeader(entry.getKey(), entry.getValue());
                }
            }
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){
                int status = response.getStatusLine().getStatusCode();
                if(status >= 200 && status<300) {
                    HttpEntity resEntity = response.getEntity();
                    if(resEntity != null){
                        result = EntityUtils.toString(resEntity);
                    }
                    resultVO.setData(result);
                    resultVO.setSuccess(true);
                }
            }
        }catch(Exception ex){
            // ex.printStackTrace();
            throw ex;
        }
        return resultVO;
    }

    public static ResultVO doGet(String url, Map<String, String> headers) throws Exception {
        ResultVO resultVO = new ResultVO();
        HttpClient httpClient ;
        HttpGet httpGet ;
        String result = null;
        try{
            httpClient = HttpClients.createDefault();
            httpGet = new HttpGet(url);
            if(headers!=null&&headers.size()>0) {
                for(Map.Entry<String, String> entry : headers.entrySet()) {
                    httpGet.addHeader(entry.getKey(), entry.getValue());
                }
            }
            HttpResponse response = httpClient.execute(httpGet);
            if(response != null){
                int status = response.getStatusLine().getStatusCode();
                if(status >= 200 && status<300) {
                    HttpEntity resEntity = response.getEntity();
                    if(resEntity != null){
                        result = EntityUtils.toString(resEntity);
                    }
                    resultVO.setData(result);
                    resultVO.setSuccess(true);
                }
            }
        }catch(Exception ex){
            //  ex.printStackTrace();
            throw ex;
        }
        return resultVO;
    }
}


