HttpUtil

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.sprucetec.datacenter.util;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.SSLContext;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpUtil {
    protected String userAgent = "";
    private int timeout = '鱀';
    private int maxConnections = 15;
    private boolean supportCompressed = true;
    RequestConfig requestConfig;
    private CloseableHttpClient httpClient;

    public String getUserAgent() {
        return this.userAgent;
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    public int getTimeout() {
        return this.timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public int getMaxConnections() {
        return this.maxConnections;
    }

    public void setMaxConnections(int maxConnections) {
        this.maxConnections = maxConnections;
    }

    public boolean isSupportCompressed() {
        return this.supportCompressed;
    }

    public void setSupportCompressed(boolean supportCompressed) {
        this.supportCompressed = supportCompressed;
    }

    public HttpUtil() {
    }

    public HttpUtil(int timeout, int maxConnections, boolean supportCompressed) {
        this.timeout = timeout;
        this.maxConnections = maxConnections;
        this.supportCompressed = supportCompressed;
    }

    public void start() {
        this.initHttpClient();
    }

    private void initHttpClient() {
        this.requestConfig = RequestConfig.custom().setContentCompressionEnabled(this.supportCompressed).setSocketTimeout(this.timeout).setConnectTimeout(this.timeout).build();
        RegistryBuilder registryBuilder = RegistryBuilder.create();
        PlainConnectionSocketFactory plainSF = new PlainConnectionSocketFactory();
        registryBuilder.register("http", plainSF);

        try {
            KeyStore registry = KeyStore.getInstance(KeyStore.getDefaultType());
            SSLContext cm = SSLContexts.custom().useTLS().loadTrustMaterial(registry, new HttpUtil.AnyTrustStrategy()).build();
            SSLConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(cm, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            registryBuilder.register("https", sslSF);
        } catch (KeyStoreException var6) {
            throw new RuntimeException(var6);
        } catch (KeyManagementException var7) {
            throw new RuntimeException(var7);
        } catch (NoSuchAlgorithmException var8) {
            throw new RuntimeException(var8);
        }

        Registry registry1 = registryBuilder.build();
        PoolingHttpClientConnectionManager cm1 = new PoolingHttpClientConnectionManager(registry1);
        cm1.setDefaultMaxPerRoute(this.maxConnections);
        cm1.setMaxTotal(this.maxConnections * 3);
        this.httpClient = HttpClients.custom().setConnectionManager(cm1).build();
    }

    public String get(String url) throws Exception {
        return this.get(url, new Header[0]);
    }

    private static List<BasicNameValuePair> builderBasicNameValuePairList(Map<String, String> requestParams) {
        if(requestParams != null && requestParams.size() > 0) {
            ArrayList list = new ArrayList(requestParams.size());
            Iterator var2 = requestParams.entrySet().iterator();

            while(var2.hasNext()) {
                Entry entry = (Entry)var2.next();
                list.add(new BasicNameValuePair((String)entry.getKey(), (String)entry.getValue()));
            }

            return list;
        } else {
            return null;
        }
    }

    public String post(String url, String data, ContentType contentType) throws Exception {
        return this.post(url, data, contentType, new Header[0]);
    }

    public String post(String url, Map requestParam) throws Exception {
        HttpPost post = new HttpPost(url);
        UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(builderBasicNameValuePairList(requestParam), "utf-8");
        post.setEntity(encodedFormEntity);
        post.setConfig(this.requestConfig);
        post.setHeader("User-Agent", this.userAgent);
        CloseableHttpResponse response = null;

        String var8;
        try {
            response = this.httpClient.execute(post);
            StatusLine status = response.getStatusLine();
            String httpresult = this.responseToString(response);
            var8 = httpresult;
        } finally {
            if(response != null) {
                response.close();
            }

        }

        return var8;
    }

    public String get(String url, Header[] headers) throws Exception {
        HttpGet get = new HttpGet(url);
        get.setConfig(this.requestConfig);
        get.setHeaders(headers);
        get.setHeader("User-Agent", this.userAgent);
        CloseableHttpResponse response = null;

        String var6;
        try {
            response = this.httpClient.execute(get);
            String httpresult = this.responseToString(response);
            var6 = httpresult;
        } finally {
            if(response != null) {
                response.close();
            }

        }

        return var6;
    }

    private String responseToString(HttpResponse response) throws Exception {
        StatusLine status = response.getStatusLine();
        HttpEntity entity = response.getEntity();
        if(status.getStatusCode() != 200) {
            throw new RuntimeException(status.getStatusCode() + "");
        } else {
            String httpresult = null;
            if(entity != null) {
                httpresult = EntityUtils.toString(entity);
            }

            return httpresult;
        }
    }

    public String post(String url, String data, ContentType contentType, Header[] headers) throws Exception {
        HttpPost post = new HttpPost(url);
        post.setHeaders(headers);
        StringEntity se = new StringEntity(data, contentType);
        post.setEntity(se);
        post.setConfig(this.requestConfig);
        post.setHeader("User-Agent", this.userAgent);
        CloseableHttpResponse response = null;

        String var10;
        try {
            response = this.httpClient.execute(post);
            StatusLine status = response.getStatusLine();
            String httpresult = this.responseToString(response);
            var10 = httpresult;
        } finally {
            if(response != null) {
                response.close();
            }

        }

        return var10;
    }

    public void close() throws IOException {
        if(this.httpClient != null) {
            this.httpClient.close();
        }

    }

    class AnyTrustStrategy implements TrustStrategy {
        AnyTrustStrategy() {
        }

        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章