有时候在每一处调用外部代码写一次httpclient的调用或者httpurlconnection代码很冗余
为了方便调用且对各种异常情况归类统计
自己目前在用的HttpUtil的代码,对于HttpClient和HttpUrlConnection的代码,包含失败率的统计,满足常规需求
如果不需要统计的,可以删除相关代码
HttpUtil代码
package com.shield.util;import java.io.IOException;import java.io.InputStream;import java.io.InterruptedIOException;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;import java.net.UnknownHostException;import java.util.HashMap;import java.util.Map;import javax.net.ssl.SSLException;import javax.net.ssl.SSLHandshakeException;import org.apache.commons.httpclient.ConnectTimeoutException;import org.apache.commons.httpclient.NoHttpResponseException;import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.http.HttpEntityEnclosingRequest;import org.apache.http.HttpRequest;import org.apache.http.client.HttpRequestRetryHandler;import org.apache.http.client.config.RequestConfig;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.client.methods.HttpRequestBase;import org.apache.http.client.protocol.HttpClientContext;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;import org.apache.http.protocol.HttpContext;import org.apache.http.util.EntityUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.aliyun.oss.common.utils.IOUtils;import com.shield.exception.CallInterfaceException;import com.shield.model.Count;/*** * 所有调用http的服务都必须使用该类 该类实现调用http服务并且统计的功能 * 统计的描述 统计字段 caller 总次数 成功次数 超时次数 * * @author slin * 2015年10月7日 */public class HttpUtil {private final static Logger LOG = LoggerFactory.getLogger(HttpUtil.class); public final static int DEFAULT_READ_TIMEOUT = 5000 ;public final static int DEFAULT_CONNECT_TIMEOUT = 2000 ;public final static PoolingHttpClientConnectionManager HTTP_CLIENT_CONNECTION_MANAGER;public static CloseableHttpClient httpClient;static{HTTP_CLIENT_CONNECTION_MANAGER = new PoolingHttpClientConnectionManager();HTTP_CLIENT_CONNECTION_MANAGER.setMaxTotal(30);HTTP_CLIENT_CONNECTION_MANAGER.setDefaultMaxPerRoute(HTTP_CLIENT_CONNECTION_MANAGER.getMaxTotal());RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(DEFAULT_READ_TIMEOUT+DEFAULT_CONNECT_TIMEOUT) .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT) .build();//请求重试处理 HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception,int executionCount, HttpContext context) { if (executionCount >= 5) {// 如果已经重试了5次,就放弃 return false; } if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试 return true; } if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常 return false; } if (exception instanceof InterruptedIOException) {// 超时 return false; } if (exception instanceof UnknownHostException) {// 目标服务器不可达 return false; } if (exception instanceof ConnectTimeoutException) {// 连接被拒绝 return false; } if (exception instanceof SSLException) {// ssl握手异常 return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); // 如果请求是幂等的,就再次尝试 if (!(request instanceof HttpEntityEnclosingRequest)) { return true; } return false; } }; httpClient = HttpClients.custom().setConnectionManager(HTTP_CLIENT_CONNECTION_MANAGER).setRetryHandler(httpRequestRetryHandler).setDefaultRequestConfig(requestConfig).build();}public final static Mapcounter = new HashMap ();public static String get(String caller,String fullurl) throws CallInterfaceException{byte [] response = getBytesHttpClientImpl(caller, fullurl,DEFAULT_READ_TIMEOUT,DEFAULT_CONNECT_TIMEOUT);LOG.debug("GET caller ="+caller+",request :"+fullurl);String responseText = null ;if(response != null && response.length > 0){responseText = new String(response);}LOG.debug("GET caller ="+caller+",response :"+responseText);return responseText;}@Deprecatedpublic static String getStringByHttpUrlConnectImpl(String caller,String fullurl) throws CallInterfaceException{byte [] response = getBytesHttpUrlConnectionImpl(caller, fullurl,DEFAULT_READ_TIMEOUT,DEFAULT_CONNECT_TIMEOUT);LOG.debug("GET caller ="+caller+",request :"+fullurl);String responseText = null ;if(response != null && response.length > 0){responseText = new String(response);}LOG.debug("GET caller ="+caller+",response :"+responseText);return responseText;}public static String getStringByHttpClientImpl(String caller,String fullurl,int readTimeout,int connectTimeout,int tryAgainSize) throws CallInterfaceException{byte [] response = getBytesHttpClientImpl(caller, fullurl,readTimeout,connectTimeout);LOG.debug("GET caller ="+caller+",request :"+fullurl);String responseText = null ;if(response != null && response.length > 0){responseText = new String(response);}LOG.debug("GET caller ="+caller+",response :"+responseText);return responseText;}public static String getStringByHttpClientImpl(String caller,String fullurl) throws CallInterfaceException{byte [] response = getBytesHttpClientImpl(caller, fullurl,DEFAULT_READ_TIMEOUT,DEFAULT_CONNECT_TIMEOUT);LOG.debug("GET caller ="+caller+",request :"+fullurl);String responseText = null ;if(response != null && response.length > 0){responseText = new String(response);}LOG.debug("GET caller ="+caller+",response :"+responseText);return responseText;}public static String getStringByHttpUrlConnectImpl(String caller,String fullurl,int readTimeout,int connectTimeout,int tryAgainSize) throws CallInterfaceException{byte [] response = getBytesHttpUrlConnectionImpl(caller, fullurl,readTimeout,connectTimeout);LOG.debug("GET caller ="+caller+",request :"+fullurl);String responseText = null ;if(response != null && response.length > 0){responseText = new String(response);}LOG.debug("GET caller ="+caller+",response :"+responseText);return responseText;}public static String postStringByHttpClientImpl(String caller,String url,String body,Header... headers) throws CallInterfaceException{byte [] response = postBytesHttpClientImpl(caller, url,body,DEFAULT_READ_TIMEOUT,DEFAULT_CONNECT_TIMEOUT,headers);LOG.debug("POST caller ="+caller+",request :"+url + "?"+body);String responseText = null ;if(response != null && response.length > 0){responseText = new String(response);}LOG.debug("POST caller ="+caller+",response :"+responseText);return responseText;}@Deprecatedpublic static String postStringByHttpUrlConnectImpl(String caller,String url,String body,Header... headers) throws CallInterfaceException{byte [] response = postBytesHttpUrlConnectionImpl(caller, url,body,DEFAULT_READ_TIMEOUT,DEFAULT_CONNECT_TIMEOUT,headers);LOG.debug("POST caller ="+caller+",request :"+url+"?"+body);String responseText = null ;if(response != null && response.length > 0){responseText = new String(response);}LOG.debug("POST caller ="+caller+",response :"+responseText);return responseText;}@Deprecatedpublic static String postStringByHttpUrlConnectImpl(String caller,String url,String body,int readTimeout,int connectTimeout, int trySize) throws CallInterfaceException{byte [] response = postBytesHttpUrlConnectionImpl(caller, url,body,readTimeout,connectTimeout);LOG.debug("POST caller ="+caller+",request :"+url+"?"+body);String responseText = null ;if(response != null && response.length > 0){responseText = new String(response);}LOG.debug("POST caller ="+caller+",response :"+responseText);return responseText;}@Deprecatedpublic static byte [] postBytesHttpUrlConnectionImpl(String caller,String url,String postData,int readTimeout,int connectTimeout, Header... headers) throws CallInterfaceException{Count count = counter.get(caller);if(count == null){count = new Count();counter.put(caller, count);}byte [] response = null ;HttpURLConnection httpConn = null ; InputStream inputStream = null; try { URL tirc = new URL(url); httpConn = (HttpURLConnection) tirc.openConnection(); httpConn.setDoOutput(true);// 使用 URL 连接进行输出 httpConn.setDoInput(true);// 使用 URL 连接进行输入 httpConn.setUseCaches(false);// 忽略缓存 httpConn.setRequestMethod("POST");// 设置URL请求方法 httpConn.setConnectTimeout(connectTimeout); httpConn.setReadTimeout(readTimeout); httpConn.setRequestProperty("Content-Length", "" + postData.length()); httpConn.setRequestProperty("Content-Type", "application/json"); httpConn.setRequestProperty("Charset", "UTF-8"); OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream(), "UTF-8"); out.write(postData); out.flush(); out.close(); int responseCode = httpConn.getResponseCode(); if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功 inputStream = httpConn.getInputStream(); response = IOUtils.readStreamAsByteArray(inputStream); } }catch (Exception e) { LOG.warn(e.getMessage() +",url="+url,e); count.timeout ++ ; throw new CallInterfaceException(e,url +"?"+ postData); }finally{ try { if(inputStream!=null) inputStream.close(); if(httpConn!=null) httpConn = null ; } catch (IOException e) { LOG.error(e.getMessage(),e); } } count.success ++ ;return response ;}public static byte [] getBytes(String caller,String fullurl) throws CallInterfaceException{return getBytesHttpClientImpl(caller, fullurl,DEFAULT_READ_TIMEOUT,DEFAULT_CONNECT_TIMEOUT);}@Deprecatedpublic static byte [] getBytesHttpUrlConnectionImpl(String caller,String fullurl,int readTimeout,int connectTimeout) throws CallInterfaceException{Count count = counter.get(caller);if(count == null){count = new Count();counter.put(caller, count);}byte [] response = null ;String url = null ;HttpURLConnection httpConn = null ; InputStream inputStream = null; try { url = fullurl; URL tirc = new URL(url); httpConn = (HttpURLConnection) tirc.openConnection(); httpConn.setDoOutput(false);// 使用 URL 连接进行输出 httpConn.setDoInput(true);// 使用 URL 连接进行输入 httpConn.setUseCaches(false);// 忽略缓存 httpConn.setRequestMethod("GET");// 设置URL请求方法 httpConn.setConnectTimeout(connectTimeout); httpConn.setReadTimeout(readTimeout); httpConn.setRequestProperty("Content-Type", "application/json"); httpConn.setRequestProperty("Charset", "UTF-8"); int responseCode = httpConn.getResponseCode(); if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功 inputStream = httpConn.getInputStream(); response = IOUtils.readStreamAsByteArray(inputStream); } }catch (Exception e) { LOG.warn(e.getMessage() +",url="+url,e); count.timeout ++ ; throw new CallInterfaceException(e,fullurl); }finally{ try { if(inputStream!=null) inputStream.close(); if(httpConn!=null) httpConn.disconnect(); httpConn = null ; } catch (IOException e) { LOG.error(e.getMessage(),e); } } count.success ++ ;return response ;}public static byte [] getBytesHttpClientImpl(String caller,String fullurl,int readTimeout,int connectTimeout) throws CallInterfaceException{Count count = counter.get(caller);if(count == null){count = new Count();counter.put(caller, count);}byte [] response = null ;CloseableHttpResponse httpResponse = null ;try{HttpGet httpGet = new HttpGet(fullurl);config(httpGet,readTimeout,connectTimeout);httpResponse = httpClient.execute(httpGet);if (HttpURLConnection.HTTP_OK == httpResponse.getStatusLine().getStatusCode()){HttpEntity entity = httpResponse.getEntity();response = EntityUtils.toByteArray(entity);}}catch(Exception e){LOG.warn(e.getMessage() +",url="+fullurl,e); count.timeout ++ ; throw new CallInterfaceException(e,fullurl);}finally{ try { if(httpResponse!=null) httpResponse.close(); } catch (IOException e) { LOG.error(e.getMessage(),e); } } count.success ++ ; return response ;}public static byte [] postBytesHttpClientImpl(String caller,String fullurl,String body,int readTimeout,int connectTimeout,Header ...headers) throws CallInterfaceException{Count count = counter.get(caller);if(count == null){count = new Count();counter.put(caller, count);}byte [] response = null ;CloseableHttpResponse httpResponse = null ;try{HttpPost httpPost = new HttpPost(fullurl);config(httpPost,readTimeout,connectTimeout);httpPost.setEntity(new StringEntity(body, "utf-8"));if(headers!=null&&headers.length>0){httpPost.setHeaders(headers);}httpResponse = httpClient.execute(httpPost);if (HttpURLConnection.HTTP_OK == httpResponse.getStatusLine().getStatusCode()){HttpEntity entity = httpResponse.getEntity();response = EntityUtils.toByteArray(entity);}}catch(Exception e){LOG.warn(e.getMessage() +",url="+fullurl,e); count.timeout ++ ; throw new CallInterfaceException(e,fullurl);}finally{ try { if(httpResponse!=null) httpResponse.close(); } catch (IOException e) { LOG.error(e.getMessage(),e); } } count.success ++ ; return response ;} private static void config(HttpRequestBase httpRequestBase, int readTimeout, int connectTimeout) { httpRequestBase.setHeader("User-Agent", "Devond HttpUtil"); httpRequestBase.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); httpRequestBase.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");//"en-US,en;q=0.5"); httpRequestBase.setHeader("Accept-Charset", "ISO-8859-1,utf-8,gbk,gb2312;q=0.7,*;q=0.7"); // 配置请求的超时设置 RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(readTimeout + connectTimeout) .setConnectTimeout(connectTimeout) .setSocketTimeout(readTimeout) .build(); httpRequestBase.setConfig(requestConfig); }}
其他相关代码
package com.shield.exception;import java.net.SocketTimeoutException;public class CallInterfaceException extends Exception{/** * */private static final long serialVersionUID = 4983997949599323781L;public CallInterfaceException(Throwable throwable, String url) {super(throwable);this.url = url ;this.message = throwable.getMessage() ;if(throwable instanceof SocketTimeoutException){if(throwable.getMessage().contains("connect")){this.type = CONNECT_TIMEOUT ;}else{this.type = READ_TIMEOUT ;}}else{this.type = EXCEPTION ;}}public CallInterfaceException(Throwable throwable,String url,int type) {super(throwable);this.url = url ;this.message = throwable.getMessage() ;this.type = type ;}private int type ; //失败类型private String url ;private String message ;public String getMessage() {return "type="+type+",message="+message+",url="+url;}public int getType() {return type;}public String getUrl() {return url;}public final static int CONNECT_TIMEOUT = 0;public final static int READ_TIMEOUT = 1 ;public final static int NOT_ASSOCIATED = 2 ;public final static int EXCEPTION = -1 ;}
package com.shield.model;import com.shield.util.DateUtil;public class Count {public int error ;public int timeout ;public int success ;public int nodata ;public int unknown ;public long initTime = DateUtil.currentTimeSeconds();public void clear(){error = 0 ;timeout = 0;success = 0 ;nodata = 0 ;unknown = 0 ;initTime = DateUtil.currentTimeSeconds();}}