jiazx0107@163.com
2023-12-07 9b5c86d852fd94c0f01497d7571b27419ab17c58
src/main/java/com/fzzy/gateway/util/GatewayHttpUtil.java
@@ -9,6 +9,11 @@
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
@@ -18,14 +23,17 @@
@Slf4j
public class GatewayHttpUtil {
    private static final int TIMEOUT_IN_MILLIONS = 5000;
    /**
     * 执行GET请求
     *
     * @param url
     * @param paramsMap
     * @return
     * @throws Exception
     */
    public static String doGet(String url,Map<String, String> paramsMap) throws Exception{
    public static String doGet(String url, Map<String, String> paramsMap) throws Exception {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
@@ -33,7 +41,7 @@
            String getUrl = url + "?";
            if (paramsMap != null) {
                for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                  //  getUrl += param.getKey() + "=" + URLEncoder.encode(param.getValue(), "UTF-8") + "&";
                    //  getUrl += param.getKey() + "=" + URLEncoder.encode(param.getValue(), "UTF-8") + "&";
                    getUrl += param.getKey() + "=" + URLEncoder.encode(param.getValue(), "UTF-8") + "&";
                }
            }
@@ -47,19 +55,68 @@
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseText = EntityUtils.toString(entity,"UTF-8");
                responseText = EntityUtils.toString(entity, "UTF-8");
            }
        } catch (Exception e) {
            log.error("http request failed", e);
            log.error("http request failed--{}", e.getMessage());
        } finally {
            try {
                response.close();
                if (null != response) response.close();
            } catch (Exception e) {
                log.error("", e);
            }
        }
        return responseText;
    }
    public static String doGet(String urlStr) {
        URL url = null;
        HttpURLConnection conn = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            url = new URL(urlStr);
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
            conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "application/json");
            if (conn.getResponseCode() == 200) {
                is = conn.getInputStream();
                baos = new ByteArrayOutputStream();
                int len = -1;
                byte[] buf = new byte[128];
                while ((len = is.read(buf)) != -1) {
                    baos.write(buf, 0, len);
                }
                baos.flush();
                return baos.toString();
            } else {
                throw new RuntimeException(" responseCode is not 200 ... ");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null)
                    is.close();
            } catch (IOException e) {
            }
            try {
                if (baos != null)
                    baos.close();
            } catch (IOException e) {
            }
            conn.disconnect();
        }
        return null;
    }
}