【HTTP请求工具类】HttpClientUtil

	public class HttpClientUtil {

    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    public static final Charset UTF8 = Charset.forName("UTF-8");

    public static final Charset GB18030 = Charset.forName("GB18030");
    /**
     * 超时时间
     */
    public static final int TIME_OUT = Integer.getInteger("http.timeout", 60000);

    private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36";

    public static HttpClient HTTP_CLIENT = buildHttpClient();

    private static HttpClient buildHttpClient() {

        SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();

        X509TrustManager tm = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] xcs, String string) {
            }
            @Override
            public void checkServerTrusted(X509Certificate[] xcs, String string) {
            }
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        try {
            SSLContext sslcontext = SSLContext.getInstance("TLS");
            sslcontext.init(null, new TrustManager[]{tm}, null);
            SSLSocketFactory socketFactory = new SSLSocketFactory(sslcontext, SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
            socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            Scheme sch = new Scheme("https", 443, socketFactory);
            schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            schemeRegistry.register(sch);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }


        PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
        cm.setMaxTotal(800);
        cm.setDefaultMaxPerRoute(200);

        cm.setMaxPerRoute(new HttpRoute(new HttpHost("localhost")), 500);
        cm.setMaxPerRoute(new HttpRoute(new HttpHost("127.0.0.1")), 500);
        HttpParams defaultParams = new BasicHttpParams();

        defaultParams.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, TIME_OUT);
        defaultParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIME_OUT);//连接超时
        defaultParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, TIME_OUT);//读取超时

        defaultParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);
        defaultParams.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, UTF8.name());
        defaultParams.setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT);


        HttpClient client = new DefaultHttpClient(cm, defaultParams);
        client = new DecompressingHttpClient(client);
        return client;
    }


    public static String get(String url, Map<String, String> HEADERS) throws IOException {
        HttpGet get = new HttpGet(url);
        return execute(get, null, HEADERS, null);
    }

    public static String get(String url,Map<String, String> params, Map<String, String> HEADERS) throws IOException, URISyntaxException {
        URIBuilder uriBuilder = new URIBuilder(url);
        if (!ObjectUtils.isEmpty(params)) {
            for(Map.Entry<String, String> entry : params.entrySet()){
                uriBuilder.addParameter(entry.getKey(), entry.getValue());
            }
        }
       HttpGet get = new HttpGet(uriBuilder.build());
        return execute(get, null, HEADERS, null);
    }

    public static String get(String url, Map<String, String> HEADERS, String proxyHost, Integer proxyPort) throws IOException {
        HttpGet get = new HttpGet(url);
        HttpHost httpHost = null;
        if (StringUtils.isNotBlank(proxyHost) && null != proxyPort) {
            httpHost = new HttpHost(proxyHost, proxyPort);
        }
        return execute(get, httpHost, HEADERS, null);
    }

    public static String get(String url, Map<String, String> HEADERS, final Charset forceCharset) throws IOException {
        HttpGet get = new HttpGet(url);
        return execute(get, null, HEADERS, forceCharset);
    }

    public static String get(String url, Map<String, String> HEADERS, final Charset forceCharset, String proxyHost, Integer proxyPort) throws IOException {
        HttpGet get = new HttpGet(url);
        HttpHost httpHost = null;
        if (StringUtils.isNotBlank(proxyHost) && null != proxyPort) {
            httpHost = new HttpHost(proxyHost, proxyPort);
        }
        return execute(get, httpHost, HEADERS, forceCharset);
    }


    public static String post(String url, Map<String, String> params, Map<String, String> headers) throws IOException {
        return post(url, params, headers, Consts.UTF_8);
    }

    public static String post(String url, Map<String, String> params, Map<String, String> headers, String proxyHost, Integer proxyPort) throws IOException {
        return post(url, params, headers, Consts.UTF_8, proxyHost, proxyPort);
    }

    public static String post(String url, Map<String, String> params, Map<String, String> headers, Charset charset) throws IOException {
        return post(url, params, headers, charset, null, null);
    }

    public static String postParam(String url,Map<String, String> params, Map<String, String> HEADERS) throws IOException, URISyntaxException {
        URIBuilder uriBuilder = new URIBuilder(url);
        if (!ObjectUtils.isEmpty(params)) {
            for(Map.Entry<String, String> entry : params.entrySet()){
                uriBuilder.addParameter(entry.getKey(), entry.getValue());
            }
        }
        HttpPost post = new HttpPost(uriBuilder.build());
        return execute(post, null, HEADERS, null);
    }

    /**
     * 非表单方式提交数据,未指定编码方式
     *
     * @param url
     * @param headers
     * @return
     * @throws IOException
     */
    public static String post(String url, String requestBody, Map<String, String> headers) throws IOException {
        HttpPost post = new HttpPost(url);
        if (StringUtils.isNotBlank(requestBody)) {
            StringEntity entity = new StringEntity(requestBody, Consts.UTF_8);
            entity.setContentType("application/json");
            post.setEntity(entity);
        }
        return execute(post, null, headers, Consts.UTF_8);
    }

    /**
     * 非表单方式提交数据,未指定编码方式
     *
     * @param url
     * @param headers
     * @return
     * @throws IOException
     */
    public static String post(String url, String requestBody, Map<String, String> headers, String proxyHost, Integer proxyPort) throws IOException {
        HttpPost post = new HttpPost(url);
        if (StringUtils.isNotBlank(requestBody)) {
            StringEntity entity = new StringEntity(requestBody, Consts.UTF_8);
            post.setEntity(entity);
        }
        HttpHost httpHost = null;
        if (StringUtils.isNotBlank(proxyHost) && null != proxyPort) {
            httpHost = new HttpHost(proxyHost, proxyPort);
        }
        return execute(post, httpHost, headers, Consts.UTF_8);
    }

    /**
     * @param url
     * @param params
     * @param headers
     * @param charset
     * @param proxyHost
     * @param proxyPort
     * @return
     * @throws IOException
     */
    public static String post(String url, Map<String, String> params, Map<String, String> headers, Charset charset, String proxyHost, Integer proxyPort) throws IOException {
        HttpPost post = new HttpPost(url);
        if (params != null && !params.isEmpty()) {
            List<NameValuePair> ps = new ArrayList<NameValuePair>(params.size());
            for (Map.Entry<String, String> kv : params.entrySet()) {
                ps.add(new BasicNameValuePair(kv.getKey(), kv.getValue()));
            }
            post.setEntity(new UrlEncodedFormEntity(ps, Consts.UTF_8));
        }
        HttpHost httpHost = null;
        if (StringUtils.isNotBlank(proxyHost) && null != proxyPort) {
            httpHost = new HttpHost(proxyHost, proxyPort);
        }
        return execute(post, httpHost, headers, charset);
    }

    private static String execute(final HttpRequestBase request, HttpHost httpHost, Map<String, String> headers, final Charset forceCharset) throws IOException {
        if (null != httpHost) {
            HTTP_CLIENT.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpHost);
        }
        return http(HTTP_CLIENT, request, headers, new HttpEntityHandler<String>() {
            @Override
            public String handle(HttpEntity entity) throws IOException {
                if (entity == null) {
                    return null;
                }
                byte[] content = EntityUtils.toByteArray(entity);
                if (forceCharset != null) {
                    return new String(content, forceCharset);
                }
                String html;
                Charset charset = null;
                ContentType contentType = ContentType.get(entity);
                if (contentType != null) {
                    charset = contentType.getCharset();
                }
                if (charset == null) {
                    charset = GB18030;
                }
                html = new String(content, charset);
                charset = checkMetaCharset(html, charset);
                if (charset != null) {
                    html = new String(content, charset);
                }
                return html;
            }
            @Override
            public String getName() {
                return request.getMethod();
            }
        });
    }

    private static <T> T http(HttpClient client, HttpRequestBase request, Map<String, String> headers, HttpEntityHandler<T> handler)
            throws IOException {
        if (headers != null && !headers.isEmpty()) {
            for (Map.Entry<String, String> kv : headers.entrySet()) {
                request.addHeader(kv.getKey(), kv.getValue());
            }
        }
        long begin = System.currentTimeMillis();
        try {
            return client.execute(request, handler, null);
        } catch (ConnectTimeoutException e) {
            logger.error(" catch ConnectTimeoutException ,closeExpiredConnections &  closeIdleConnections for 30 s. ");
            client.getConnectionManager().closeExpiredConnections();
            client.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
            throw e;
        } finally {
            logger.info(handler.getName() + " " + request.getURI() + " ,cost:" + (System.currentTimeMillis() - begin) + "ms");
        }
    }

    /**
     * 字符编码检查
     *
     * @param html
     * @param use
     * @return
     */
    private static Charset checkMetaCharset(String html, Charset use) {
        String magic = "charset=";
        int index = html.indexOf(magic);
        if (index > 0 && index < 1000) {
            index += magic.length();
            int end = html.indexOf('"', index);
            if (end > index) {
                try {
                    String charSetString = html.substring(index, end).toLowerCase();
                    if (charSetString.length() > 10) {
                        return null;
                    }
                    //GBK GB2312 --> GB18030
                    if (charSetString.startsWith("gb")) {
                        return GB18030.equals(use) ? null : GB18030;
                    }
                    Charset curr = Charset.forName(charSetString);
                    if (!curr.equals(use)) {
                        return curr;
                    }
                } catch (Exception e) {
                    logger.error("Get MetaCharset error", e);
                }
            }
        }
        return null;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火火笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值