SuperHttpDao.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package net.diaowen.common.dao;
  2. import org.apache.http.Header;
  3. import org.apache.http.HttpStatus;
  4. import org.apache.http.StatusLine;
  5. import org.apache.http.client.ClientProtocolException;
  6. import org.apache.http.client.config.RequestConfig;
  7. import org.apache.http.client.methods.*;
  8. import org.apache.http.client.utils.URIBuilder;
  9. import org.apache.http.impl.client.CloseableHttpClient;
  10. import org.apache.http.util.EntityUtils;
  11. import org.apache.logging.log4j.LogManager;
  12. import org.apache.logging.log4j.Logger;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Component;
  15. import java.io.IOException;
  16. import java.net.URISyntaxException;
  17. import java.util.Map;
  18. //import org.slf4j.Logger;
  19. //import org.slf4j.LoggerFactory;
  20. /**
  21. *
  22. * @ClassName: SuperHttpDao
  23. * @Description: TODO(http请求超类,http请求更高级原始的封装)
  24. * @author keyuan
  25. * @date 2016年9月17日 上午11:22:21
  26. *
  27. */
  28. @Component
  29. public class SuperHttpDao {
  30. private static Logger logger = LogManager.getLogger(SuperHttpDao.class.getName());
  31. @Autowired
  32. protected CloseableHttpClient httpClient;
  33. @Autowired
  34. protected RequestConfig requestConfig;
  35. public String doGet(HttpGet httpGet) {
  36. CloseableHttpResponse response = null;
  37. try {
  38. // 执行请求
  39. response = httpClient.execute(httpGet);
  40. // 判断返回状态是否为200
  41. StatusLine statusLine = response.getStatusLine();
  42. if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
  43. return EntityUtils.toString(response.getEntity(), "UTF-8");
  44. }
  45. } catch (ClientProtocolException e) {
  46. logger.error("doGet(HttpGet httpGet) ClientProtocolException : {} ", httpGet.getURI().toString());
  47. } catch (IOException e) {
  48. logger.error("doGet(HttpGet httpGet) IOException : {} ", httpGet.getURI().toString());
  49. } finally {
  50. if (response != null) {
  51. try {
  52. response.close();
  53. } catch (IOException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59. return null;
  60. }
  61. public String doPost(HttpPost httpPost) {
  62. CloseableHttpResponse response = null;
  63. try {
  64. // 执行请求
  65. response = httpClient.execute(httpPost);
  66. // 判断返回状态是否为200
  67. if (response.getStatusLine().getStatusCode() == 200) {
  68. return EntityUtils.toString(response.getEntity(), "UTF-8");
  69. }
  70. } catch (ClientProtocolException e) {
  71. e.printStackTrace();
  72. logger.error("doPost(HttpPost httpPost) ClientProtocolException : {} ", httpPost.getURI().toString());
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. logger.error("doPost(HttpPost httpPost) IOException : {} ", httpPost.getURI().toString());
  76. } finally {
  77. if (response != null) {
  78. try {
  79. response.close();
  80. } catch (IOException e) {
  81. // TODO Auto-generated catch block
  82. e.printStackTrace();
  83. }
  84. }
  85. }
  86. return null;
  87. }
  88. public String doDelete(HttpDelete httpDelete) {
  89. CloseableHttpResponse response = null;
  90. try {
  91. // 执行请求
  92. response = httpClient.execute(httpDelete);
  93. // 判断返回状态是否为200
  94. if (response.getStatusLine().getStatusCode() == 200) {
  95. return EntityUtils.toString(response.getEntity(), "UTF-8");
  96. }
  97. } catch (ClientProtocolException e) {
  98. logger.error("doDelete(HttpDelete httpDelete) ClientProtocolException : {} ", httpDelete.getURI().toString());
  99. } catch (IOException e) {
  100. logger.error("doDelete(HttpDelete httpDelete) IOException : {} ", httpDelete.getURI().toString());
  101. } finally {
  102. if (response != null) {
  103. try {
  104. response.close();
  105. } catch (IOException e) {
  106. // TODO Auto-generated catch block
  107. e.printStackTrace();
  108. }
  109. }
  110. }
  111. return null;
  112. }
  113. public String doPatch(HttpPatch httpPatch) {
  114. CloseableHttpResponse response = null;
  115. try {
  116. Header[] headers2 = httpPatch.getAllHeaders();
  117. // 执行请求
  118. response = httpClient.execute(httpPatch);
  119. // 判断返回状态是否为200
  120. if (response.getStatusLine().getStatusCode() == 200) {
  121. return EntityUtils.toString(response.getEntity(), "UTF-8");
  122. }
  123. } catch (ClientProtocolException e) {
  124. logger.error("doPatch(HttpPatch httpPatch) ClientProtocolException : {} ", httpPatch.getURI().toString());
  125. } catch (IOException e) {
  126. logger.error("doPatch(HttpPatch httpPatch) IOException : {} ", httpPatch.getURI().toString());
  127. } finally {
  128. if (response != null) {
  129. try {
  130. response.close();
  131. } catch (IOException e) {
  132. // TODO Auto-generated catch block
  133. e.printStackTrace();
  134. }
  135. }
  136. }
  137. return null;
  138. }
  139. public String doPut(HttpPut httpPut) {
  140. CloseableHttpResponse response = null;
  141. try {
  142. // 执行请求
  143. response = httpClient.execute(httpPut);
  144. // 判断返回状态是否为200
  145. if (response.getStatusLine().getStatusCode() == 200) {
  146. return EntityUtils.toString(response.getEntity(), "UTF-8");
  147. }
  148. } catch (ClientProtocolException e) {
  149. logger.error("doPatch(HttpPatch httpPatch) ClientProtocolException : {} ", httpPut.getURI().toString());
  150. } catch (IOException e) {
  151. logger.error("doPatch(HttpPatch httpPatch) IOException : {} ", httpPut.getURI().toString());
  152. } finally {
  153. if (response != null) {
  154. try {
  155. response.close();
  156. } catch (IOException e) {
  157. // TODO Auto-generated catch block
  158. e.printStackTrace();
  159. }
  160. }
  161. }
  162. return null;
  163. }
  164. /* (non-Javadoc)
  165. * @see net.diaowen.youwe.dao.impl.SuperHttpDao#doGet(java.lang.String)
  166. */
  167. public String doGet(String url) {
  168. // 创建http GET请求
  169. HttpGet httpGet = new HttpGet(url);
  170. httpGet.setConfig(this.requestConfig);
  171. return doGet(httpGet);
  172. }
  173. /* (non-Javadoc)
  174. * @see net.diaowen.youwe.dao.impl.SuperHttpDao#doGet(java.lang.String, java.util.Map)
  175. */
  176. public String doGet(String url, Map<String, String> params) {
  177. try {
  178. URIBuilder uriBuilder = new URIBuilder(url);
  179. for (String key : params.keySet()) {
  180. uriBuilder.addParameter(key, params.get(key));
  181. }
  182. return this.doGet(uriBuilder.build().toString());
  183. } catch (URISyntaxException e) {
  184. logger.error("doGet(String url, Map<String, String> params) URISyntaxException : {} ", url);
  185. }
  186. return null;
  187. }
  188. public String buildUrl(String url, Map<String, String> params) {
  189. try {
  190. URIBuilder uriBuilder = new URIBuilder(url);
  191. for (String key : params.keySet()) {
  192. uriBuilder.addParameter(key, params.get(key));
  193. }
  194. return uriBuilder.build().toString();
  195. } catch (URISyntaxException e) {
  196. logger.error("doGet(String url, Map<String, String> params) URISyntaxException : {} ", url);
  197. }
  198. return null;
  199. }
  200. /* (non-Javadoc)
  201. * @see net.diaowen.youwe.dao.impl.SuperHttpDao#doPost(java.lang.String, java.util.Map)
  202. */
  203. /*
  204. public HttpResult doPost(String url, Map<String, String> params) throws IOException {
  205. // 创建http POST请求
  206. HttpPost httpPost = new HttpPost(url);
  207. httpPost.setConfig(this.requestConfig);
  208. if (params != null) {
  209. // 设置2个post参数,一个是scope、一个是q
  210. List<NameValuePair> parameters = new ArrayList<NameValuePair>();
  211. for (String key : params.keySet()) {
  212. parameters.add(new BasicNameValuePair(key, params.get(key)));
  213. }
  214. // 构造一个form表单式的实体
  215. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
  216. // 将请求实体设置到httpPost对象中
  217. httpPost.setEntity(formEntity);
  218. }
  219. CloseableHttpResponse response = null;
  220. try {
  221. // 执行请求
  222. response = httpClient.execute(httpPost);
  223. return new HttpResult(response.getStatusLine().getStatusCode(),
  224. EntityUtils.toString(response.getEntity(), "UTF-8"));
  225. } finally {
  226. if (response != null) {
  227. response.close();
  228. }
  229. }
  230. }
  231. */
  232. /* (non-Javadoc)
  233. * @see net.diaowen.youwe.dao.impl.SuperHttpDao#doPost(java.lang.String)
  234. */
  235. // public HttpResult doPost(String url) throws IOException {
  236. // return this.doPost(url, null);
  237. // }
  238. /* (non-Javadoc)
  239. * @see net.diaowen.youwe.dao.impl.SuperHttpDao#doPostJson(java.lang.String, java.lang.String)
  240. */
  241. /*
  242. public HttpResult doPostJson(String url, String json) throws ClientProtocolException, IOException {
  243. // 创建http POST请求
  244. HttpPost httpPost = new HttpPost(url);
  245. httpPost.setConfig(this.requestConfig);
  246. if (json != null) {
  247. // 构造一个form表单式的实体
  248. StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
  249. // 将请求实体设置到httpPost对象中
  250. httpPost.setEntity(stringEntity);
  251. }
  252. CloseableHttpResponse response = null;
  253. try {
  254. // 执行请求
  255. response = this.httpClient.execute(httpPost);
  256. return new HttpResult(response.getStatusLine().getStatusCode(),EntityUtils.toString(response.getEntity(), "UTF-8"));
  257. } finally {
  258. if (response != null) {
  259. response.close();
  260. }
  261. }
  262. }
  263. */
  264. }