import org.apache.http.*; 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.ConnectionKeepAliveStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; /** * Https请求工具类 * @author DUCHONG * @since 2018-05-03 13:04:42 */ public class HttpUtils { private static Logger logger=LoggerFactory.getLogger(HttpUtils.class); //private static final CloseableHttpClient httpclient = HttpClients.createDefault(); private static CloseableHttpClient httpclient =null; static { ConnectionKeepAliveStrategy connectionKeepAliveStrategy = new ConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) { return 20 * 1000; // tomcat默认keepAliveTimeout为20s } }; PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(20, TimeUnit.SECONDS); connManager.setMaxTotal(200); connManager.setDefaultMaxPerRoute(200); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(10 * 1000) .setSocketTimeout(10 * 1000) .setConnectionRequestTimeout(10 * 1000) .build(); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setConnectionManager(connManager); httpClientBuilder.setDefaultRequestConfig(requestConfig); httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler()); httpClientBuilder.setKeepAliveStrategy(connectionKeepAliveStrategy); //HttpClient httpClient = httpClientBuilder.build(); httpclient=httpClientBuilder.build(); } /** * 发送HttpGet请求 * @param url * @return */ public static String sendGet(String url) { HttpGet httpget = new HttpGet(url); CloseableHttpResponse response = null; try { response = httpclient.execute(httpget); } catch (IOException e1) { e1.printStackTrace(); } String result = null; try { HttpEntity entity = response.getEntity(); if (entity != null) { result = EntityUtils.toString(entity); } } catch (ParseException | IOException e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } return result; } /** * 发送HttpPost请求,参数为map * @param url * @param map * @return */ public static String sendPost(String url, Map<String, String> map) { List<NameValuePair> formParams = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> entry : map.entrySet()) { formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8); HttpPost httppost = new HttpPost(url); httppost.setEntity(entity); CloseableHttpResponse response = null; try { response = httpclient.execute(httppost); } catch (IOException e) { e.printStackTrace(); } HttpEntity entity1 = response.getEntity(); String result = null; try { result = EntityUtils.toString(entity1); } catch (ParseException | IOException e) { e.printStackTrace(); } return result; } /** * 发送不带参数的HttpPost请求 * @param url * @return */ public static String sendPost(String url) { HttpPost httppost = new HttpPost(url); CloseableHttpResponse response = null; try { response = httpclient.execute(httppost); } catch (IOException e) { e.printStackTrace(); } HttpEntity entity = response.getEntity(); String result = null; try { result = EntityUtils.toString(entity); } catch (ParseException | IOException e) { e.printStackTrace(); } return result; } /** * 编码 * @param bstr * @return String */ public static String encode(byte[] bstr){ return new sun.misc.BASE64Encoder().encode(bstr); } /** * 解码 * @param str * @return string */ public static byte[] decode(String str){ byte[] bt = null; try { sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder(); bt = decoder.decodeBuffer( str ); } catch (IOException e) { e.printStackTrace(); } return bt; } }
<!--httpclient--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.4</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4</version> </dependency>
Commons的HttpClient项目现在是生命的尽头,不再被开发。它已取代由Apache HttpComponents项目HttpClient和的HttpCore模组,提供更好的性能和更大的灵活性。