{ private static PoolingHttpClientConnectionManager connectionManager = null; private static RequestConfig config = null; static { connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(2048); //可以细分每一个域名最大的连接数 connectionManager.setDefaultMaxPerRoute(512); //设置请求参数 config = RequestConfig.custom().setConnectTimeout(2000) //连接超时时间 .setConnectionRequestTimeout(500) //从线程池中获取线程超时时间 .setSocketTimeout(2000) //设置数据超时时间 .build(); } public static String postMothed(String url){ return postMothed(url,null); } public static String postMothed(String url, Map<String,String> params){ HttpPost httpPost = new HttpPost(url); if(params != null) { List list = new ArrayList(); for(String k : params.keySet()){ list.add(new BasicNameValuePair(k, params.get(k))); } HttpEntity requestEntity = null; try { requestEntity = new UrlEncodedFormEntity(list, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } httpPost.setEntity(requestEntity); } return executeHttpClient(httpPost); } public static String getMothed(String url){ HttpGet httpGet = new HttpGet(url); return executeHttpClient(httpGet); } private static String executeHttpClient(HttpRequestBase httpRequest){ String content = null; //从连接池取httpclient CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build(); httpRequest.setConfig(config); HttpResponse response = null; try { response = httpClient.execute(httpRequest); //根据http code做业务判断 System.out.println(response.getStatusLine().getStatusCode()); content = EntityUtils.toString(response.getEntity(), "utf-8"); //System.out.println(content); } catch (IOException e) { e.printStackTrace(); }finally { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } return content; } public static void main(String[] args) throws IOException { } }