基于HttpClient4.5.1实现Http访问工具类

本工具类基于httpclient4.5.1实现,点击此处查看官方版HttpClient4.5.1手册

1. pom.xml中依赖包设置

[html]  view plain  copy
  1. <dependency>  
  2.     <groupId>org.apache.httpcomponents</groupId>  
  3.     <artifactId>httpclient</artifactId>  
  4.     <version>4.5.1</version>  
  5. </dependency>  

2. HttpClient工具类

[java]  view plain  copy
  1. import java.io.IOException;  
  2. import java.io.UnsupportedEncodingException;  
  3. import java.net.URISyntaxException;  
  4. import java.util.ArrayList;  
  5. import java.util.Map;  
  6.   
  7. import org.apache.http.HttpEntity;  
  8. import org.apache.http.NameValuePair;  
  9. import org.apache.http.client.ClientProtocolException;  
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  11. import org.apache.http.client.methods.CloseableHttpResponse;  
  12. import org.apache.http.client.methods.HttpGet;  
  13. import org.apache.http.client.methods.HttpPost;  
  14. import org.apache.http.client.methods.HttpRequestBase;  
  15. import org.apache.http.client.utils.URIBuilder;  
  16. import org.apache.http.impl.client.CloseableHttpClient;  
  17. import org.apache.http.impl.client.HttpClients;  
  18.   
  19. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;  
  20. import org.apache.http.message.BasicNameValuePair;  
  21. import org.apache.http.util.EntityUtils;  
  22.   
  23. /** 
  24.  *  
  25.  * @author Nan 2015-11 
  26.  */  
  27. public class HttpClientUtil {  
  28.     private static PoolingHttpClientConnectionManager cm;  
  29.     private static String EMPTY_STR = "";  
  30.     private static String UTF_8 = "UTF-8";  
  31.   
  32.     private static void init() {  
  33.         if (cm == null) {  
  34.             cm = new PoolingHttpClientConnectionManager();  
  35.             cm.setMaxTotal(50);// 整个连接池最大连接数  
  36.             cm.setDefaultMaxPerRoute(5);// 每路由最大连接数,默认值是2  
  37.         }  
  38.     }  
  39.   
  40.     /** 
  41.      * 通过连接池获取HttpClient 
  42.      *  
  43.      * @return 
  44.      */  
  45.     private static CloseableHttpClient getHttpClient() {  
  46.         init();  
  47.         return HttpClients.custom().setConnectionManager(cm).build();  
  48.     }  
  49.   
  50.     /** 
  51.      *  
  52.      * @param url 
  53.      * @return 
  54.      */  
  55.     public static String httpGetRequest(String url) {  
  56.         HttpGet httpGet = new HttpGet(url);  
  57.         return getResult(httpGet);  
  58.     }  
  59.   
  60.     public static String httpGetRequest(String url, Map<String, Object> params) throws URISyntaxException {  
  61.         URIBuilder ub = new URIBuilder();  
  62.         ub.setPath(url);  
  63.   
  64.         ArrayList<NameValuePair> pairs = covertParams2NVPS(params);  
  65.         ub.setParameters(pairs);  
  66.   
  67.         HttpGet httpGet = new HttpGet(ub.build());  
  68.         return getResult(httpGet);  
  69.     }  
  70.   
  71.     public static String httpGetRequest(String url, Map<String, Object> headers, Map<String, Object> params)  
  72.             throws URISyntaxException {  
  73.         URIBuilder ub = new URIBuilder();  
  74.         ub.setPath(url);  
  75.   
  76.         ArrayList<NameValuePair> pairs = covertParams2NVPS(params);  
  77.         ub.setParameters(pairs);  
  78.   
  79.         HttpGet httpGet = new HttpGet(ub.build());  
  80.         for (Map.Entry<String, Object> param : headers.entrySet()) {  
  81.             httpGet.addHeader(param.getKey(), String.valueOf(param.getValue()));  
  82.         }  
  83.         return getResult(httpGet);  
  84.     }  
  85.   
  86.     public static String httpPostRequest(String url) {  
  87.         HttpPost httpPost = new HttpPost(url);  
  88.         return getResult(httpPost);  
  89.     }  
  90.   
  91.     public static String httpPostRequest(String url, Map<String, Object> params) throws UnsupportedEncodingException {  
  92.         HttpPost httpPost = new HttpPost(url);  
  93.         ArrayList<NameValuePair> pairs = covertParams2NVPS(params);  
  94.         httpPost.setEntity(new UrlEncodedFormEntity(pairs, UTF_8));  
  95.         return getResult(httpPost);  
  96.     }  
  97.   
  98.     public static String httpPostRequest(String url, Map<String, Object> headers, Map<String, Object> params)  
  99.             throws UnsupportedEncodingException {  
  100.         HttpPost httpPost = new HttpPost(url);  
  101.   
  102.         for (Map.Entry<String, Object> param : headers.entrySet()) {  
  103.             httpPost.addHeader(param.getKey(), String.valueOf(param.getValue()));  
  104.         }  
  105.   
  106.         ArrayList<NameValuePair> pairs = covertParams2NVPS(params);  
  107.         httpPost.setEntity(new UrlEncodedFormEntity(pairs, UTF_8));  
  108.   
  109.         return getResult(httpPost);  
  110.     }  
  111.   
  112.     private static ArrayList<NameValuePair> covertParams2NVPS(Map<String, Object> params) {  
  113.         ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();  
  114.         for (Map.Entry<String, Object> param : params.entrySet()) {  
  115.             pairs.add(new BasicNameValuePair(param.getKey(), String.valueOf(param.getValue())));  
  116.         }  
  117.   
  118.         return pairs;  
  119.     }  
  120.   
  121.     /** 
  122.      * 处理Http请求 
  123.      *  
  124.      * @param request 
  125.      * @return 
  126.      */  
  127.     private static String getResult(HttpRequestBase request) {  
  128.         // CloseableHttpClient httpClient = HttpClients.createDefault();  
  129.         CloseableHttpClient httpClient = getHttpClient();  
  130.         try {  
  131.             CloseableHttpResponse response = httpClient.execute(request);  
  132.             // response.getStatusLine().getStatusCode();  
  133.             HttpEntity entity = response.getEntity();  
  134.             if (entity != null) {  
  135.                 // long len = entity.getContentLength();// -1 表示长度未知  
  136.                 String result = EntityUtils.toString(entity);  
  137.                 response.close();  
  138.                 // httpClient.close();  
  139.                 return result;  
  140.             }  
  141.         } catch (ClientProtocolException e) {  
  142.             e.printStackTrace();  
  143.         } catch (IOException e) {  
  144.             e.printStackTrace();  
  145.         } finally {  
  146.   
  147.         }  
  148.   
  149.         return EMPTY_STR;  
  150.     }  
  151.   
  152. }  

猜你喜欢

转载自blog.csdn.net/qq_42039607/article/details/80389774
今日推荐