httpUtil 工具类
/** * Created by xucd on 2017/6/28. */ public class HttpUtil { /** * 构建httpPost对象 * @param vo * @return */ public static HttpPost createHttpPost(HttpVO vo){ HttpPost httpPost = new HttpPost(vo.getUrl()); // 设置超时 RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(1500) .setConnectTimeout(1500) .setSocketTimeout(1500).build(); httpPost.setConfig(requestConfig); httpPost.setEntity(vo.getEntity()); return httpPost; } /** * http post请求 * @param vo * @return * @throws Exception */ public static HttpReturnBO post(HttpVO vo) throws Exception{ HttpReturnBO httpReturnBO = new HttpReturnBO(); CloseableHttpClient httpclient = null; HttpPost httpPost = null; CloseableHttpResponse response = null; try { httpclient = HttpClients.createDefault(); httpPost = HttpUtil.createHttpPost(vo); response = httpclient.execute(httpPost); httpReturnBO = HttpUtil.createReturnBo(response); }catch(Exception e){ httpReturnBO.setStatus(false); httpReturnBO.setMessage("请求发生异常,网络异常(50001)"); e.printStackTrace(); System.out.println("post请求地址异常,url:" + vo.getUrl()); } finally { if(response!=null){ response.close(); } if (httpPost != null) { httpPost.abort(); } if(httpclient!=null){ httpclient.close(); } } return httpReturnBO; } /** * 构建httpGet对象 * @param vo * @return */ public static HttpGet createHttpGet(HttpVO vo){ HttpGet httpGet = new HttpGet(vo.getUrl()); // 设置超时 RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(1500) .setConnectTimeout(1500) .setSocketTimeout(1500).build(); httpGet.setConfig(requestConfig); return httpGet; } /** * http get请求 * @param vo * @return * @throws Exception */ public static HttpReturnBO get(HttpVO vo) throws Exception{ HttpReturnBO httpReturnBO = new HttpReturnBO(); CloseableHttpClient httpclient = null; HttpGet httpGet = null; CloseableHttpResponse response = null; try { httpclient = HttpClients.createDefault(); httpGet = HttpUtil.createHttpGet(vo); response = httpclient.execute(httpGet); httpReturnBO = HttpUtil.createReturnBo(response); }catch(Exception e){ httpReturnBO.setStatus(false); httpReturnBO.setMessage("请求发生异常,网络异常(50001)"); e.printStackTrace(); System.out.println("get请求地址异常,url:" + vo.getUrl()); } finally { if(response!=null){ response.close(); } if (httpGet != null) { httpGet.abort(); } if(httpclient!=null){ httpclient.close(); } } return httpReturnBO; } /** * 创建http请求返回参数对象 * @param response * @return * @throws Exception */ public static HttpReturnBO createReturnBo(CloseableHttpResponse response) throws Exception{ HttpReturnBO httpReturnBO = new HttpReturnBO(); int code = response.getStatusLine().getStatusCode(); if (code == 200) { httpReturnBO.setStatus(true); String rev = EntityUtils.toString(response.getEntity()); httpReturnBO.setMessage(rev); }else{ httpReturnBO.setStatus(false); httpReturnBO.setMessage("请求失败,网络异常("+code+")"); } return httpReturnBO; } }
HttpReturnBO 业务类
/** * Created by xucd on 2017/6/28. */ public class HttpReturnBO { // 1:请求正常;2:请求异常 private boolean status = false; private String message = "网络异常(50001)"; public boolean isStatus() { return status; } public void setStatus(boolean status) { this.status = status; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
HttpVO 类
/** * Created by xucd on 2017/6/28. */ public class HttpVO implements HttpInterfaceVO{ // 请求地址 private String url; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } @Override public UrlEncodedFormEntity getEntity() { return null; } }
HttpInterfaceVO 接口
/** * Created by xucd on 2017/6/28. */ public interface HttpInterfaceVO { /** * 获取http要带的参数 * @return */ UrlEncodedFormEntity getEntity(); }
环境配置需要
pom.xml依赖
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.6</version> </dependency>