apache httpclient工具类 发表于 2017-01-06 | 更新于 2017-01-06 | 分类于 http | httpclient的简单封装 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158public class IntelliHttpClientUtils { private static final Logger logger = LoggerFactory.getLogger(IntelliHttpClientUtils.class); //HttpClient private volatile static CloseableHttpClient httpClient; //设置请求和传输超时时间 1000*6 秒 private static final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(0b1011101110000).setConnectTimeout(0b1011101110000).build(); /** * HTTP GET * * @param url * @param encode * @return */ public static String httpGet(String url, String encode) { if (encode == null) encode = "utf-8"; String resultStr = null; CloseableHttpClient closeableHttpClient = getHttpClient(); HttpGet httpGet = new HttpGet(url); httpGet.setConfig(requestConfig); CloseableHttpResponse httpResponse = null; try { httpResponse = closeableHttpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() < HttpStatus.SC_BAD_REQUEST) { HttpEntity entity = httpResponse.getEntity(); resultStr = EntityUtils.toString(entity, encode); } //请求失败 直接返回null } catch (Exception e) { logger.info("Intelli.IntelliHttpClientUtils.httpGet 产生异常:{}", e.getMessage()); } finally { HttpClientUtils.closeQuietly(httpResponse); } return resultStr; } /** * HTTP POST 请求 * * @param url * @param encode * @return */ public static String httpPost(String url, Map<String, String> params, String encode) { if (encode == null) { encode = "utf-8"; } CloseableHttpClient closeableHttpClient = getHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); //组织请求参数 List<NameValuePair> paramList = new ArrayList<NameValuePair>(); Set<String> keySet = params.keySet(); for (String key : keySet) { paramList.add(new BasicNameValuePair(key, params.get(key))); } try { httpPost.setEntity(new UrlEncodedFormEntity(paramList, encode)); } catch (UnsupportedEncodingException e1) { logger.info("Intelli.IntelliHttpClientUtils.httpPost (Map请求添加参数时)产生异常:{}", e1.getMessage()); return null; } String resultStr = getResponse2Str(encode, closeableHttpClient, httpPost); return resultStr; } /** * Http Post 请求 * * @param url * @param jsonStr * @param encode * @return */ public static String httpPost(String url, String jsonStr, String encode) { if (encode == null) { encode = "utf-8"; } CloseableHttpClient closeableHttpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); httpPost.setHeader("Content-type", "application/json"); //组织请求参数 StringEntity stringEntity = new StringEntity(jsonStr, encode); httpPost.setEntity(stringEntity); String resultStr = getResponse2Str(encode, closeableHttpClient, httpPost); return resultStr; } /** * 获取 CloseableHttpClient * * @return */ private static CloseableHttpClient getHttpClient() { if (httpClient == null) { synchronized (IntelliHttpClientUtils.class) { if (httpClient == null) { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setConnectionManager(new BasicHttpClientConnectionManager()); httpClient = httpClientBuilder.build(); } } } return httpClient; } /** * 关闭httpClient */ private void closeHttpClient() { if (IntelliHttpClientUtils.httpClient == null) return; HttpClientUtils.closeQuietly(IntelliHttpClientUtils.httpClient); } /*** * 关闭httpClient */ @Override protected void finalize() throws Throwable { this.closeHttpClient(); super.finalize(); } /** * 执行 POST 请求 * * @param encode * @param closeableHttpClient * @param httpPost * @return */ private static String getResponse2Str(String encode, CloseableHttpClient closeableHttpClient, HttpPost httpPost) { CloseableHttpResponse httpResponse = null; String resultStr = null; try { httpResponse = closeableHttpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() < HttpStatus.SC_BAD_REQUEST) { HttpEntity entity = httpResponse.getEntity(); resultStr = EntityUtils.toString(entity, encode); } } catch (Exception e) { logger.info("Intelli.IntelliHttpClientUtils.getResponse2Str 产生异常:{}", e.getMessage()); } finally { HttpClientUtils.closeQuietly(httpResponse); } return resultStr; }} End 坚持原创技术分享,您的支持将鼓励我继续创作! 赏 微信打赏 支付宝打赏