RequestForwardUtil

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

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.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RequestForwardUtil {
private static Logger log = LoggerFactory.getLogger(RequestForwardUtil.class);
private static RequestConfig config = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();

public RequestForwardUtil() {
}

public static String httpGet(String url, Map headerMap) throws IOException {
    CloseableHttpClient client = null;
    HttpGet httpGet = new HttpGet(url);
    String result = null;

    try {
        client = HttpClients.createDefault();
        httpGet.setConfig(config);
        if (headerMap != null && !headerMap.isEmpty()) {
            headerMap.entrySet().forEach((entry) -> {
                httpGet.setHeader(new BasicHeader((String)entry.getKey(), (String)entry.getValue()));
            });
        }

        CloseableHttpResponse response = client.execute(httpGet);
        result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
    } finally {
        if (httpGet != null) {
            httpGet.releaseConnection();
        }

        if (client != null) {
            client.close();
        }

    }

    return result;
}

public static String httpPost(String url, String params, Map headerMap) throws IOException {
    CloseableHttpClient client = null;
    HttpPost httpPost = new HttpPost(url);
    String result = null;

    try {
        client = HttpClients.createDefault();
        httpPost.setConfig(config);
        if (headerMap != null && !headerMap.isEmpty()) {
            headerMap.entrySet().forEach((entry) -> {
                httpPost.setHeader((String)entry.getKey(), (String)entry.getValue());
            });
        }

        StringEntity entity = new StringEntity(params, StandardCharsets.UTF_8);
        entity.setContentType(new BasicHeader("Content-Encoding", StandardCharsets.UTF_8.name()));
        entity.setContentType(new BasicHeader("Content-Type", "application/json"));
        httpPost.setEntity(entity);
        CloseableHttpResponse response = client.execute(httpPost);
        log.debug(String.valueOf(response.getStatusLine().getStatusCode()));
        result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
    } finally {
        if (httpPost != null) {
            httpPost.releaseConnection();
        }

        if (client != null) {
            client.close();
        }

    }

    return result;
}

public static String httpPut(String url, String params, Map headerMap) throws IOException {
    CloseableHttpClient client = null;
    HttpPut httpPut = new HttpPut(url);
    String result = null;

    try {
        client = HttpClients.createDefault();
        httpPut.setConfig(config);
        if (headerMap != null && !headerMap.isEmpty()) {
            headerMap.entrySet().forEach((entry) -> {
                httpPut.setHeader((String)entry.getKey(), (String)entry.getValue());
            });
        }

        StringEntity entity = new StringEntity(params, StandardCharsets.UTF_8);
        entity.setContentType(new BasicHeader("Content-Encoding", StandardCharsets.UTF_8.name()));
        entity.setContentType(new BasicHeader("Content-Type", "application/json"));
        httpPut.setEntity(entity);
        CloseableHttpResponse response = client.execute(httpPut);
        result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
    } finally {
        if (httpPut != null) {
            httpPut.releaseConnection();
        }

        if (client != null) {
            client.close();
        }

    }

    return result;
}

public static String httpDelete(String url, Map headerMap) throws IOException {
    CloseableHttpClient client = null;
    HttpDelete httpDelete = new HttpDelete(url);
    String result = null;

    try {
        client = HttpClients.createDefault();
        httpDelete.setConfig(config);
        if (headerMap != null && !headerMap.isEmpty()) {
            headerMap.entrySet().forEach((entry) -> {
                httpDelete.setHeader((String)entry.getKey(), (String)entry.getValue());
            });
        }

        CloseableHttpResponse response = client.execute(httpDelete);
        result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
    } finally {
        if (httpDelete != null) {
            httpDelete.releaseConnection();
        }

        if (client != null) {
            client.close();
        }

    }

    return result;
}

public static String httpSSLGet(String url, Map headerMap) throws IOException, KeyManagementException, NoSuchAlgorithmException {
    CloseableHttpClient client = null;
    HttpGet httpGet = new HttpGet(url);
    String result = null;

    try {
        client = sslClient();
        httpGet.setConfig(config);
        if (headerMap != null && !headerMap.isEmpty()) {
            headerMap.entrySet().forEach((entry) -> {
                httpGet.setHeader(new BasicHeader((String)entry.getKey(), (String)entry.getValue()));
            });
        }

        CloseableHttpResponse response = client.execute(httpGet);
        HttpEntity httpEntity = response.getEntity();
        if (null != httpEntity) {
            result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        }
    } finally {
        if (httpGet != null) {
            httpGet.releaseConnection();
        }

        if (client != null) {
            client.close();
        }

    }

    return result;
}

public static String httpSSLPost(String url, String params, Map headerMap) throws IOException, KeyManagementException, NoSuchAlgorithmException {
    CloseableHttpClient client = null;
    HttpPost httpPost = new HttpPost(url);
    String result = null;
    try {
        client = sslClient();
        if (headerMap != null && !headerMap.isEmpty()) {
            headerMap.entrySet().forEach((entry) -> {
                httpPost.setHeader((String)entry.getKey(), (String)entry.getValue());
            });
        }

        httpPost.setConfig(config);
        StringEntity entity = new StringEntity(params, StandardCharsets.UTF_8);
        entity.setContentEncoding(new BasicHeader("Content-Encoding", StandardCharsets.UTF_8.name()));
        entity.setContentType(new BasicHeader("Content-Type", "application/json"));
        httpPost.setEntity(entity);
        CloseableHttpResponse response = client.execute(httpPost);
        HttpEntity httpEntity = response.getEntity();
        if (null != httpEntity) {
            result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        }
    } finally {
        if (httpPost != null) {
            httpPost.releaseConnection();
        }

        if (client != null) {
            client.close();
        }

    }

    return result;
}

public static String httpSSLPut(String url, String params, Map headerMap) throws IOException, KeyManagementException, NoSuchAlgorithmException {
    CloseableHttpClient client = null;
    HttpPut httpPut = new HttpPut(url);
    String result = null;

    try {
        client = sslClient();
        if (headerMap != null && !headerMap.isEmpty()) {
            headerMap.entrySet().forEach((entry) -> {
                httpPut.setHeader((String)entry.getKey(), (String)entry.getValue());
            });
        }

        httpPut.setConfig(config);
        StringEntity entity = new StringEntity(params, StandardCharsets.UTF_8);
        entity.setContentEncoding(new BasicHeader("Content-Encoding", StandardCharsets.UTF_8.name()));
        entity.setContentType(new BasicHeader("Content-Type", "application/json"));
        httpPut.setEntity(entity);
        CloseableHttpResponse response = client.execute(httpPut);
        HttpEntity httpEntity = response.getEntity();
        if (null != httpEntity) {
            result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        }
    } finally {
        if (httpPut != null) {
            httpPut.releaseConnection();
        }

        if (client != null) {
            client.close();
        }

    }

    return result;
}

public static String httpSSLDelete(String url, Map headerMap) throws IOException, KeyManagementException, NoSuchAlgorithmException {
    CloseableHttpClient client = null;
    HttpDelete httpDelete = new HttpDelete(url);
    String result = null;

    try {
        client = sslClient();
        httpDelete.setConfig(config);
        if (headerMap != null && !headerMap.isEmpty()) {
            headerMap.entrySet().forEach((entry) -> {
                httpDelete.setHeader(new BasicHeader((String)entry.getKey(), (String)entry.getValue()));
            });
        }

        CloseableHttpResponse response = client.execute(httpDelete);
        HttpEntity entity = response.getEntity();
        if (null != entity) {
            result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        }
    } finally {
        if (httpDelete != null) {
            httpDelete.releaseConnection();
        }

        if (client != null) {
            client.close();
        }

    }

    return result;
}

public static String getForwardUrl(StringBuffer orginalUrl, String serverName, String serverPath, Map parameterMap) {
    if (parameterMap != null && !parameterMap.isEmpty()) {
        orginalUrl.append("?");
        StringBuffer params = new StringBuffer();
        parameterMap.entrySet().stream().map((entry) -> {
            StringBuffer param = new StringBuffer();
            Arrays.stream(entry.getValue()).forEach((value) -> {
                try {
                    value = java.net.URLEncoder.encode(value, "UTF-8");
                } catch (UnsupportedEncodingException var4) {
                    var4.printStackTrace();
                }

                param.append((String)entry.getKey() + "=" + value + "&");
            });
            return param.deleteCharAt(param.length() - 1).toString();
        }).forEach((param) -> {
            params.append(param + "&");
        });
        params.deleteCharAt(params.length() - 1);
        orginalUrl.append(params);
    }

    return orginalUrl.toString();
}

public static String getBodyData(BufferedReader reader) throws IOException {
    StringBuilder params = new StringBuilder();

    try {
        char[] buff = new char[1024];

        int len;
        while((len = reader.read(buff)) != -1) {
            params.append(buff, 0, len);
        }
    } finally {
        if (reader != null) {
            reader.close();
        }

    }

    return params.toString();
}

private static CloseableHttpClient sslClient() throws NoSuchAlgorithmException, KeyManagementException {
    SSLContext ctx = SSLContext.getInstance("TLS");
    X509TrustManager tm = new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }

        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }
    };
    ctx.init((KeyManager[])null, new TrustManager[]{tm}, (SecureRandom)null);
    SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
    return HttpClients.custom().setSSLSocketFactory(ssf).build();
}

}

你可能感兴趣的:(HTTP)