HTTP4.3 工具类、多线程下的连接池的使用

最近用到多线程来对请求HTTP , 但是http的每次请求都是一次连接后重新连接,导致大量的数据积压,所以根据资料,重新写了一个HTTP的工具类

package com.zto.medivh.common.util;

import java.io.UnsupportedEncodingException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
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.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpHostConnectException;
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.DefaultHttpRequestRetryHandler;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.StringUtils;


public class HttpUtils {

	private static Logger logger = LoggerFactory.getLogger(HttpUtils.class);
	static final String TIMEOUT = "timeout";

	private static final CloseableHttpClient httpClient;
	public static final String CHARSET_GBK = "GBK";
	public static final String CHARSET_UTF8 = "UTF-8";
	// 链接建立的超时时间
	public static final int REQUEST_TIMEOUT = 1500;
	// 响应超时时间,超过此时间不再读取响应;
	public static final int REQUEST_SOCKET_TIME = 1500;
	// http clilent中从connetcion pool中获得一个connection的超时时间;
	public static final int REQUEST_REQUEST_TIME=1500;

	static {
		// RequestConfig config =
		// RequestConfig.custom().setConnectTimeout(60000)
		// .setSocketTimeout(15000).build();
		// httpClient =
		// HttpClientBuilder.create().setDefaultRequestConfig(config)
		// .build();
		PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
		// 配置开发环境连接数
		// 设置最大连接数
		// 设置最大路由连接数 
		if (CommonUtils.isDev() || CommonUtils.isTest()) {
			cm.setMaxTotal(50);
			cm.setDefaultMaxPerRoute(5);
		} else {
			cm.setMaxTotal(10000);
			cm.setDefaultMaxPerRoute(600);
		}

		RequestConfig requestConfig = RequestConfig.custom()
				.setConnectTimeout(REQUEST_TIMEOUT)
				// 使用fiddler 抓取请求时开启该配置
				// .setProxy(new HttpHost("127.0.0.1", 8888,"http"))
				.setSocketTimeout(REQUEST_SOCKET_TIME).build();

		httpClient = HttpClients.custom().setConnectionManager(cm)
				.setDefaultRequestConfig(requestConfig)

				.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))
				.disableRedirectHandling().build();

	}

	public static HttpResEntity doGet(String url, Map<String, String> params,
			String charset) {
		HttpResEntity entity = new HttpResEntity();
		if (StringUtils.isBlank(url)) {
			entity.setMsg("请求地址异常");
			entity.setStatus(404);
			return entity;
		}
		try {
			if (params != null && !params.isEmpty()) {
				List<NameValuePair> pairs = new ArrayList<NameValuePair>(
						params.size());
				for (Map.Entry<String, String> entry : params.entrySet()) {
					Object value = entry.getValue();
					if (value != null) {
						pairs.add(new BasicNameValuePair(entry.getKey(),
								(String) value));
					}
				}
				url += "?"
						+ EntityUtils.toString(new UrlEncodedFormEntity(pairs,
								charset));
			}
			HttpGet httpGet = new HttpGet(url);
			CloseableHttpResponse response = httpClient.execute(httpGet);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != 200) {
				httpGet.abort();
				entity.setData("请求异常");
				entity.setStatus(statusCode);
				return entity;
			}
			HttpEntity httpEntity = response.getEntity();
			String result = null;
			if (entity != null) {
				result = EntityUtils.toString(httpEntity, charset);
			}
			EntityUtils.consume(httpEntity);
			response.close();
			entity.setStatus(statusCode);
			entity.setData(result);
			return entity;
		} catch (SocketTimeoutException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (ConnectTimeoutException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (HttpHostConnectException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (SocketException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (Exception e) {
			logger.info(e.toString() + url + "," + e.getMessage());
			entity.setMsg(url + ":" + e.toString());
			entity.setStatus(-1);
			return entity;
		}
	}

	public static HttpResEntity doPost(String url, Map<String, String> params,
			String charset) {
		List<NameValuePair> pairs = null;
		if (params != null && !params.isEmpty()) {
			pairs = new ArrayList<NameValuePair>(params.size());
			for (Map.Entry<String, String> entry : params.entrySet()) {
				Object value = entry.getValue();
				if (value != null) {
					pairs.add(new BasicNameValuePair(entry.getKey(),
							(String) value));
				}
			}
		}
		StringEntity httpEntity = null;
		try {
			httpEntity = new UrlEncodedFormEntity(pairs, charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return doPost(url, httpEntity, charset);
	}

	public static HttpResEntity doPost(String url, Map<String, String> params,
			String charset, String contentType) {
		List<NameValuePair> pairs = null;
		if (params != null && !params.isEmpty()) {
			pairs = new ArrayList<NameValuePair>(params.size());
			for (Map.Entry<String, String> entry : params.entrySet()) {
				Object value = entry.getValue();
				if (value != null) {
					pairs.add(new BasicNameValuePair(entry.getKey(),
							(String) value));
				}
			}
		}
		StringEntity httpEntity = null;
		try {
			httpEntity = new UrlEncodedFormEntity(pairs, charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return doPost(url, httpEntity, charset, contentType);
	}

	public static HttpResEntity doPost(String url, String params,
			String charset, String contentType, boolean isMap) {
		StringEntity entity = null;
		Map<String, String> map = JsonUtil.parseNotThrowException(params,
				Map.class);

		return doPost(url, map, charset);
	}

	public static HttpResEntity doPost(String url, String params, String charset) {
		StringEntity entity = null;
		entity = new StringEntity(params, charset);

		return doPost(url, entity, charset);
	}

	public static HttpResEntity doPostToJson(String url, String params,
			String charset) {
		StringEntity entity = null;
		try {
			entity = new StringEntity(params,
					ContentType.APPLICATION_JSON.getMimeType(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return doPost(url, entity, charset);
	}

	public static HttpResEntity doPost(String url, String params,
			String charset, String contentType) {
		StringEntity entity = null;
		entity = new StringEntity(params, charset);

		return doPost(url, entity, charset, contentType);
	}

	/**
	 * HTTP Post 获取内容
	 * 
	 * @param url
	 *            请求的url地址 ?之前的地址
	 * @param params
	 *            请求的参数
	 * @param charset
	 *            编码格式
	 * @return 页面内容
	 */
	private static HttpResEntity doPost(String url, StringEntity params,
			String charset, String contentType) {
		HttpResEntity entity = new HttpResEntity();
		if (StringUtils.isBlank(url)) {
			entity.setMsg("请求地址异常");
			entity.setStatus(404);
			return entity;
		}
		try {
			HttpPost httpPost = new HttpPost(url);
			if (StringUtils.isNoneEmpty(contentType)) {
				httpPost.setHeader("Content-Type", "application/json");
			}
			if (params != null) {
				httpPost.setEntity(params);

			}
			CloseableHttpResponse response = httpClient.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != 200) {
				httpPost.abort();
				entity.setData("请求异常");
				entity.setStatus(statusCode);
				return entity;
			}
			HttpEntity httpEntity = response.getEntity();
			String result = null;
			if (httpEntity != null) {
				result = EntityUtils.toString(httpEntity, charset);
			}
			EntityUtils.consume(httpEntity);
			response.close();
			entity.setStatus(statusCode);
			entity.setData(result);
			return entity;
		} catch (SocketTimeoutException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (ConnectTimeoutException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (HttpHostConnectException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (SocketException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (Exception e) {
			logger.info(e.toString() + url + "," + e.getMessage());
			entity.setMsg(url + ":" + e.toString());
			entity.setStatus(-1);
			return entity;
		}
	}

	/**
	 * HTTP Post 获取内容
	 * 
	 * @param url
	 *            请求的url地址 ?之前的地址
	 * @param params
	 *            请求的参数
	 * @param charset
	 *            编码格式
	 * @return 页面内容
	 */
	public static HttpResEntity doPost(String url, StringEntity params,
			String charset) {
		HttpResEntity entity = new HttpResEntity();
		if (StringUtils.isBlank(url)) {
			entity.setMsg("请求地址异常");
			entity.setStatus(404);
			return entity;
		}
		try {

			HttpPost httpPost = new HttpPost(url);
			if (params != null) {
				httpPost.setEntity(params);

			}
			CloseableHttpResponse response = httpClient.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != 200) {
				httpPost.abort();
				entity.setData("请求异常");
				entity.setStatus(statusCode);
				return entity;
			}
			HttpEntity httpEntity = response.getEntity();
			String result = null;
			if (httpEntity != null) {
				result = EntityUtils.toString(httpEntity, charset);
			}
			EntityUtils.consume(httpEntity);
			response.close();
			entity.setStatus(statusCode);
			entity.setData(result);
			return entity;
		} catch (SocketTimeoutException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (ConnectTimeoutException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (HttpHostConnectException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (SocketException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (Exception e) {
			logger.info(e.toString() + url + "," + e.getMessage());
			entity.setMsg(url + ":" + e.toString());
			entity.setStatus(-1);
			return entity;
		}
	}

	/**
	 * HTTP Post 获取内容
	 * 
	 * @param url
	 *            请求的url地址 ?之前的地址
	 * @param params
	 *            请求的参数
	 * @param charset
	 *            编码格式
	 * @return 页面内容
	 */
	public static HttpResEntity post(String url, StringEntity params,
			String charset) {
		HttpResEntity entity = new HttpResEntity();
		if (StringUtils.isBlank(url)) {
			entity.setMsg("请求地址异常");
			entity.setStatus(404);
			return entity;
		}
		try {
			HttpPost httpPost = new HttpPost(url);
			if (params != null) {
				httpPost.setEntity(params);
			}
			CloseableHttpResponse response = httpClient.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != 200) {
				httpPost.abort();
				entity.setData("请求异常");
				entity.setStatus(statusCode);
				return entity;
			}
			HttpEntity httpEntity = response.getEntity();
			String result = null;
			if (httpEntity != null) {
				result = EntityUtils.toString(httpEntity, charset);
			}
			EntityUtils.consume(httpEntity);
			response.close();
			entity.setStatus(statusCode);
			entity.setData(result);
			return entity;
		} catch (SocketTimeoutException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (ConnectTimeoutException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (HttpHostConnectException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (SocketException e) {
			logger.info("请求超时:" + url + "," + e.getMessage());
			entity.setMsg(url + ":" + HttpUtils.TIMEOUT);
			entity.setStatus(-1);
			return entity;
		} catch (Exception e) {
			logger.info(e.toString() + url + "," + e.getMessage());
			entity.setMsg(url + ":" + e.toString());
			entity.setStatus(-1);
			return entity;
		}
	}

	public static HttpResEntity post(String url, Map<String, String> params,
			String charset) {
		List<NameValuePair> pairs = null;
		if (params != null && !params.isEmpty()) {
			pairs = new ArrayList<NameValuePair>(params.size());
			for (Map.Entry<String, String> entry : params.entrySet()) {
				Object value = entry.getValue();
				if (value != null) {
					pairs.add(new BasicNameValuePair(entry.getKey(),
							(String) value));
				}
			}
		}
		StringEntity httpEntity = null;
		try {
			httpEntity = new UrlEncodedFormEntity(pairs, charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return post(url, httpEntity, charset);
	}

	/**
	 * 发送POST 请求
	 * 
	 * @param url
	 *            请求地址
	 * @param charset
	 *            编码格式
	 * @param params
	 *            请求参数
	 * @return 响应
	 * @throws IOException
	 */
	public static String post(String url, String charset,
			Map<String, String> params, String sign) throws IOException {

		HttpURLConnection conn = null;
		OutputStreamWriter out = null;
		InputStream inputStream = null;
		InputStreamReader inputStreamReader = null;
		BufferedReader reader = null;
		StringBuffer result = new StringBuffer();
		try {
			conn = (HttpURLConnection) new URL(url).openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("Accept-Charset", charset);
			conn.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");

			out = new OutputStreamWriter(conn.getOutputStream(), charset);
			String data = buildQuery(params, charset);
			data += "&sign=" + sign;
			out.write(data);
			out.flush();
			inputStream = conn.getInputStream();
			inputStreamReader = new InputStreamReader(inputStream);
			reader = new BufferedReader(inputStreamReader);
			String tempLine = null;
			while ((tempLine = reader.readLine()) != null) {
				result.append(tempLine);
			}

		} catch (IOException e) {
			e.printStackTrace();
			logger.info("请求超时:" + url, e);
			return null;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		} finally {
			if (out != null) {
				out.close();
			}
			if (reader != null) {
				reader.close();
			}
			if (inputStreamReader != null) {
				inputStreamReader.close();
			}
			if (inputStream != null) {
				inputStream.close();
			}
		}
		return result.toString();
	}

	/**
	 * 将map转换为请求字符串
	 * <p>
	 * data=xxx&msg_type=xxx
	 * </p>
	 * 
	 * @param params
	 * @param charset
	 * @return
	 * @throws IOException
	 */
	public static String buildQuery(Map<String, String> params, String charset)
			throws IOException {
		if (params == null || params.isEmpty()) {
			return null;
		}

		StringBuffer data = new StringBuffer();
		boolean flag = false;

		for (Entry<String, String> entry : params.entrySet()) {
			if (flag) {
				data.append("&");
			} else {
				flag = true;
			}

			data.append(entry.getKey())
					.append("=")
					.append(URLEncoder.encode(entry.getValue() == null ? ""
							: entry.getValue(), charset));
		}

		return data.toString();

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值