HttpClient工具类

和上一篇RestTemplat一样,也是用于测试接口

就可以在测试类中,直接调用HttpClientUtil中的方法,传入url,和方法参数就可以返回方法返回值了。非常的简单。

封装HttpClient工具  

  1 package model;
  2 
  3 import java.io.IOException;
  4 import java.util.Map;
  5 import java.util.Map.Entry;
  6 
  7 import org.apache.http.Consts;
  8 import org.apache.http.HttpEntity;
  9 import org.apache.http.ParseException;
 10 import org.apache.http.client.config.RequestConfig;
 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.entity.ContentType;
 15 import org.apache.http.entity.StringEntity;
 16 import org.apache.http.impl.client.CloseableHttpClient;
 17 import org.apache.http.impl.client.HttpClients;
 18 import org.apache.http.util.EntityUtils;
 19 
 20 import net.sf.json.JSONObject;
 21 
 22 /**
 23  * @author GWCheng
 24  *
 25  */
 26 public class HttpUtil {
 27 
 28     private static final CloseableHttpClient httpclient = HttpClients.createDefault();
 29 
 30     /**
 31      * 发送HttpGet请求
 32      * 
 33      * @param url
 34      * @return
 35      */
 36     public static String sendGet(String url) {
 37 
 38         HttpGet httpget = new HttpGet(url);
 39         CloseableHttpResponse response = null;
 40         try {
 41             response = httpclient.execute(httpget);
 42         } catch (IOException e1) {
 43             e1.printStackTrace();
 44         }
 45         String result = null;
 46         try {
 47             HttpEntity entity = response.getEntity();
 48             if (entity != null) {
 49                 result = EntityUtils.toString(entity);
 50             }
 51         } catch (ParseException | IOException e) {
 52             e.printStackTrace();
 53         } finally {
 54             try {
 55                 response.close();
 56             } catch (IOException e) {
 57                 e.printStackTrace();
 58             }
 59         }
 60         return result;
 61     }
 62 
 63     /**
 64      * 发送HttpPost请求,参数为map
 65      * 
 66      * @param url
 67      * @param map
 68      * @return
 69      */
 70     public static String sendPost(String url, Map<String, Object> map) {
 71         // 初始化post
 72         HttpPost httppost = new HttpPost(url);
 73         RequestConfig build = RequestConfig.custom().setExpectContinueEnabled(false).build();
 74         httppost.setConfig(build);
 75         JSONObject jsonObject = JSONObject.fromObject(map);
 76         StringEntity stringEntity = new StringEntity(jsonObject.toString(),
 77                 ContentType.create("application/json", Consts.UTF_8));
 78         httppost.setEntity(stringEntity);
 79         CloseableHttpResponse response = null;
 80         try {
 81             response = httpclient.execute(httppost);
 82         } catch (IOException e) {
 83             e.printStackTrace();
 84         }
 85         HttpEntity entity1 = response.getEntity();
 86         String result = null;
 87         try {
 88             result = EntityUtils.toString(entity1);
 89         } catch (ParseException | IOException e) {
 90             e.printStackTrace();
 91         }
 92         return result;
 93     }
 94 
 95     public static CloseableHttpResponse sendPostWithHeader(String url, Map<String, Object> map, Map<String, Object> headers) {
 96         // 初始化post
 97         HttpPost httppost = new HttpPost(url);
 98         RequestConfig build = RequestConfig.custom().setExpectContinueEnabled(false).build();
 99         httppost.setConfig(build);
100         JSONObject jsonObject = JSONObject.fromObject(map);
101         StringEntity stringEntity = new StringEntity(jsonObject.toString(),
102                 ContentType.create("application/json", Consts.UTF_8));
103         httppost.setEntity(stringEntity);
104         if (headers != null && headers.size() > 0) {
105             for (Entry e : headers.entrySet()) {
106                 httppost.setHeader((String) e.getKey(), (String) e.getValue());
107             }
108         }
109 
110         CloseableHttpResponse response = null;
111         try {
112             return httpclient.execute(httppost);
113         } catch (IOException e) {
114             e.printStackTrace();
115         }
116         return null;
117     }
118 
119     /**
120      * 发送不带参数的HttpPost请求
121      * 
122      * @param url
123      * @return
124      */
125     public static String sendPost(String url) {
126         HttpPost httppost = new HttpPost(url);
127         RequestConfig build = RequestConfig.custom().setExpectContinueEnabled(false).build();
128         httppost.setConfig(build);
129         httppost.addHeader("Content-Type", "application/json");
130         CloseableHttpResponse response = null;
131         try {
132             response = httpclient.execute(httppost);
133 
134         } catch (IOException e) {
135             e.printStackTrace();
136         }
137         HttpEntity entity = response.getEntity();
138         String result = null;
139         try {
140             result = EntityUtils.toString(entity);
141         } catch (ParseException | IOException e) {
142             e.printStackTrace();
143         }
144         return result;
145     }
146 
147 }

 

posted @ 2017-11-10 17:23  _童话boy  阅读(1028)  评论(0编辑  收藏  举报