<!--google guava 限流-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
package com.yung.user.util; import com.google.common.util.concurrent.RateLimiter; import com.yung.user.dto.HttpResult; import org.apache.commons.collections.MapUtils; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; /** Tools * http request * / public class HTTPUTIL { Private static Logger LOGGER = Final LoggerFactory.getLogger (HttpUtil.class); Private Final static String URL_PARAM_CONNECT_EQUAL_SIGN = "="; Private Final static String URL_PARAM_CONNECT_FLAG = "&"; Private Final static String = HTTP_CONTENT_TYPE_KEY "the ContentType"; Private static MultiThreadedHttpConnectionManager ConnectionManager = null; / ** * connection time * / Private static int ConnectionTimeout = 2000; / ** * timeout time * / Private socketTimeout static int = 2000; /** * the maximum number of connections * / Private maxConnectionPerHost static int = 30; / ** * The maximum number of active connections * / Private maxTotalConnections static int = 30; Private Client static HttpClient; / ** * token bucket restrictor * 300 generate second tokens * / Private static rateLimiter rateLimiter = RateLimiter.create (300); static { ConnectionManager MultiThreadedHttpConnectionManager new new = (); . connectionManager.getParams () setConnectionTimeout (ConnectionTimeout); . connectionManager.getParams () setSoTimeout (socketTimeout); connectionManager.getParams () .setDefaultMaxConnectionsPerHost (maxConnectionPerHost); . connectionManager.getParams () setMaxTotalConnections (maxTotalConnections); Client = new new HttpClient (ConnectionManager); } / ** * POST method to submit data * @param url URL to be requested data to be submitted * @param params * @return response result * / public static HttpResult the doPost (String URL, the Map <String, String> the params) { // Get token. 1 seconds without deny access IF (! rateLimiter.tryAcquire (. 1, TimeUnit.SECONDS)) { the throw new new BusinessException (the ErrorStatus. GET_LIMIT_TOKEN_FAIL_ERROR); } HttpResult new new HttpResult Result = (); a PostMethod PostMethod = null; the try { PostMethod a PostMethod new new = (URL); IF (StringUtils.isEmpty (params.get (HTTP_CONTENT_TYPE_KEY))) { postMethod.setRequestHeader ( "the Content-the Type", "file application / X-WWW-form-urlencoded; charset = the UTF8"); } IF (MapUtils.isNotEmpty (the params)) { the Set <of Map.Entry <String, String entries It >> params.entrySet = (); for (of Map.Entry <String, String> entry: entries It) { postMethod.addParameter (entry.getKey (), entry.getValue ()); } } client.executeMethod (PostMethod); Result. setStatusCode (postMethod.getStatusCode ()); result.setData (postMethod.getResponseBodyAsString ()); } catch (HttpException e) { LOGGER.error ( "fatal exception occurs, the protocol may not be a problem or content return", e); } catch (IOException e) { ; LOGGER.error ( "exception occurs Network", E) } {the finally IF (PostMethod = null!) { PostMethod.releaseConnection (); } } return Result; } / ** * GET method to submit data * @param url requests to be the URL data to be submitted * @param params * @return response result * / public static HttpResult the doGet (String URL, the Map <String, String> the params) { // the token. 1 seconds without obtaining access is denied if (! rateLimiter. to tryAcquire (. 1, TimeUnit.SECONDS)) { the throw new new BusinessException (ErrorStatus.GET_LIMIT_TOKEN_FAIL_ERROR); } HttpResult new new HttpResult Result = (); the getMethod getMethod = null; GetHttpGetParamsStr httpGetParamsStr = String (the params); IF (StringUtils.isNotEmpty (httpGetParamsStr)) { URL = + + httpGetParamsStr URL "?"; } The try { getMethod the GetMethod new new = (URL); IF (StringUtils.isEmpty (params.get (HTTP_CONTENT_TYPE_KEY ))) { "; charset = the UTF8 file application / X-WWW-form-urlencoded"); ( "the Type-the Content", getMethod.setRequestHeader } client.executeMethod (getMethod); result.setStatusCode(getMethod.getStatusCode()); result.setData (getMethod.getResponseBodyAsString ()); the catch} (HttpException the E) { LOGGER.error ( "fatal exception occurs, the protocol may not be a problem or content return", e); } catch (IOException e) { LOGGER.error("发生网络异常", e); } finally { if (getMethod != null) { getMethod.releaseConnection(); } } return result; } /** * 获取get请求参数字符串 * @param params * @return */ private static String getHttpGetParamsStr(Map<String, String> params) { if (MapUtils.isEmpty(params)) { return null; } StringBuilder builder = new StringBuilder(); Set<Map.Entry<String, String>> entries = params.entrySet(); int i = 0; for (Map.Entry<String, String> entry : entries) { builder.append(entry.getKey()).append(URL_PARAM_CONNECT_EQUAL_SIGN).append(entry.getValue()); if (i != entries.size() - 1) { builder.append(URL_PARAM_CONNECT_FLAG); } i++; } return builder.toString(); } }