com.ykun.commons.utils.http.HttpClientUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.ykun.commons.utils.http.HttpClientUtils.java

Source

/*
 * Commons-Utils
 * Copyright (c) 2017.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 */

package com.ykun.commons.utils.http;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.fluent.Executor;
import org.apache.http.client.fluent.Request;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static com.ykun.commons.utils.constant.Constant.CHARSET_UTF8;

/**
 * ??HttpClient 4.5.x
 *
 * @author Ykun  2017-03-28 10:20
 */
public class HttpClientUtils {

    private final static Log logger = LogFactory.getLog(HttpClientUtils.class);

    /**
     * ??
     */
    private final static int CONNECT_TIME_OUT = 3000;

    /**
     * ??
     */
    private final static int SOCKET_TIME_OUT = 5000;

    /**
     * ?
     */
    private final static int RETRY_TIMES = 3;

    /**
     * ?
     */
    private final static int MAX_TOTAL = 500;

    /**
     * ?
     */
    private final static int MAX_PER_ROUTE = 100;

    /**
     * ???socket?
     */
    private final static int VALIDATE_AFTER_INACTIVITY = 1000;

    private final static String EMPTY_STRING = "";
    private final static String SYMBOL_MARK = "?";
    private final static String SYMBOL_CONNECTOR = "&";
    private final static String SYMBOL_EQUAL = "=";

    private static Executor executor;

    /**
     * ?HttpClientconnectionManager?
     */
    static {
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setDefaultMaxPerRoute(MAX_PER_ROUTE);
        connectionManager.setMaxTotal(MAX_TOTAL);
        connectionManager.setValidateAfterInactivity(VALIDATE_AFTER_INACTIVITY);

        RequestConfig.Builder builder = RequestConfig.custom();
        builder.setConnectTimeout(CONNECT_TIME_OUT);
        builder.setSocketTimeout(SOCKET_TIME_OUT);
        HttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(builder.build())
                .setConnectionManager(connectionManager).setRetryHandler(new RetryHandler()).build();
        executor = Executor.newInstance(httpClient);
    }

    public static String post(String url, Header[] headers, Map<String, String> params) {
        return execute(Request.Post(url).bodyForm(map2List(params), Consts.UTF_8).setHeaders(headers));
    }

    public static String post(String url, Map<String, String> params) {
        return execute(Request.Post(url).bodyForm(map2List(params), Consts.UTF_8));
    }

    public static String post(String url) {
        return execute(Request.Post(url));
    }

    public static String get(String url, Header[] headers, Map<String, String> params) {
        return execute(Request.Get(parse(url, params)).setHeaders(headers));
    }

    public static String get(String url, Map<String, String> params) {
        return execute(Request.Get(parse(url, params)));
    }

    public static String get(String url) {
        return execute(Request.Get(url));
    }

    /**
     * Url?
     */
    private static String parse(String url, Map<String, String> params) {
        if (params == null && params.size() == 0) {
            return url;
        }
        StringBuilder builder = new StringBuilder(url);
        builder.append(SYMBOL_MARK);
        Iterator<String> it = params.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            try {
                builder.append(key).append(SYMBOL_EQUAL).append(
                        URLEncoder.encode(params.get(key) == null ? EMPTY_STRING : params.get(key), CHARSET_UTF8))
                        .append(SYMBOL_CONNECTOR);
            } catch (UnsupportedEncodingException e) {
                logger.error("Parse Url error", e);
                throw new RuntimeException(e);
            }
        }
        String parse = builder.toString();
        return parse.substring(0, parse.length() - 1);
    }

    /**
     * ??
     */
    private static List<NameValuePair> map2List(Map<String, String> params) {
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        if (params != null && params.size() > 0) {
            Iterator<String> it = params.keySet().iterator();
            while (it.hasNext()) {
                String name = it.next();
                list.add(new BasicNameValuePair(name, params.get(name) == null ? EMPTY_STRING : params.get(name)));
            }
        }
        return list;
    }

    /**
     * request
     *
     * @param request Request
     * @return String
     */
    public static String execute(Request request) {
        try {
            return executor.execute(request).returnContent().asString(Consts.UTF_8);
        } catch (Exception e) {
            logger.error("Execute request error", e);
        }
        return null;
    }

    /**
     * ?
     */
    private static class RetryHandler implements HttpRequestRetryHandler {
        public boolean retryRequest(IOException exception, int executionCount, HttpContext httpContext) {
            // ?3
            if (executionCount > RETRY_TIMES) {
                return false;
            }
            return true;
        }
    }

}