HttpClientUtil.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package com.gyee.datatraining.util;
  2. import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
  3. import org.apache.commons.httpclient.HttpClient;
  4. import org.apache.commons.httpclient.HttpStatus;
  5. import org.apache.commons.httpclient.NameValuePair;
  6. import org.apache.commons.httpclient.methods.GetMethod;
  7. import org.apache.commons.httpclient.methods.PostMethod;
  8. import org.apache.commons.httpclient.params.HttpMethodParams;
  9. import java.io.*;
  10. import java.util.Iterator;
  11. import java.util.Map;
  12. import java.util.Map.Entry;
  13. import java.util.Set;
  14. /**
  15. * @ClassName : HttpClientUtil
  16. * @Author : xieshengjie
  17. * @Date: 2021/12/22 15:52
  18. * @Description :
  19. */
  20. public class HttpClientUtil {
  21. public static String doGet(String url) {
  22. // 输入流
  23. InputStream is = null;
  24. BufferedReader br = null;
  25. String result = null;
  26. // 创建httpClient实例
  27. HttpClient httpClient = new HttpClient();
  28. // 设置http连接主机服务超时时间:15000毫秒
  29. // 先获取连接管理器对象,再获取参数对象,再进行参数的赋值
  30. httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
  31. // 创建一个Get方法实例对象
  32. GetMethod getMethod = new GetMethod(url);
  33. // 设置get请求超时为60000毫秒
  34. getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
  35. // 设置请求重试机制,默认重试次数:3次,参数设置为true,重试机制可用,false相反
  36. getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, true));
  37. try {
  38. // 执行Get方法
  39. int statusCode = httpClient.executeMethod(getMethod);
  40. // 判断返回码
  41. if (statusCode != HttpStatus.SC_OK) {
  42. // 如果状态码返回的不是ok,说明失败了,打印错误信息
  43. System.err.println("Method faild: " + getMethod.getStatusLine());
  44. } else {
  45. // 通过getMethod实例,获取远程的一个输入流
  46. is = getMethod.getResponseBodyAsStream();
  47. // 包装输入流
  48. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  49. StringBuffer sbf = new StringBuffer();
  50. // 读取封装的输入流
  51. String temp = null;
  52. while ((temp = br.readLine()) != null) {
  53. sbf.append(temp).append("\r\n");
  54. }
  55. result = sbf.toString();
  56. }
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. } finally {
  60. // 关闭资源
  61. if (null != br) {
  62. try {
  63. br.close();
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. if (null != is) {
  69. try {
  70. is.close();
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. // 释放连接
  76. getMethod.releaseConnection();
  77. }
  78. return result;
  79. }
  80. public static String doPost(String url, Map<String, Object> paramMap) {
  81. // 获取输入流
  82. InputStream is = null;
  83. BufferedReader br = null;
  84. String result = null;
  85. // 创建httpClient实例对象
  86. HttpClient httpClient = new HttpClient();
  87. // 设置httpClient连接主机服务器超时时间:15000毫秒
  88. httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
  89. // 创建post请求方法实例对象
  90. PostMethod postMethod = new PostMethod(url);
  91. // 设置post请求超时时间
  92. postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
  93. NameValuePair[] nvp = null;
  94. // 判断参数map集合paramMap是否为空
  95. if (null != paramMap && paramMap.size() > 0) {// 不为空
  96. // 创建键值参数对象数组,大小为参数的个数
  97. nvp = new NameValuePair[paramMap.size()];
  98. // 循环遍历参数集合map
  99. Set<Entry<String, Object>> entrySet = paramMap.entrySet();
  100. // 获取迭代器
  101. Iterator<Entry<String, Object>> iterator = entrySet.iterator();
  102. int index = 0;
  103. while (iterator.hasNext()) {
  104. Entry<String, Object> mapEntry = iterator.next();
  105. // 从mapEntry中获取key和value创建键值对象存放到数组中
  106. try {
  107. nvp[index] = new NameValuePair(mapEntry.getKey(),
  108. new String(mapEntry.getValue().toString().getBytes("UTF-8"), "UTF-8"));
  109. } catch (UnsupportedEncodingException e) {
  110. e.printStackTrace();
  111. }
  112. index++;
  113. }
  114. }
  115. // 判断nvp数组是否为空
  116. if (null != nvp && nvp.length > 0) {
  117. // 将参数存放到requestBody对象中
  118. postMethod.setRequestBody(nvp);
  119. }
  120. // 执行POST方法
  121. try {
  122. int statusCode = httpClient.executeMethod(postMethod);
  123. // 判断是否成功
  124. if (statusCode != HttpStatus.SC_OK) {
  125. System.err.println("Method faild: " + postMethod.getStatusLine());
  126. }
  127. // 获取远程返回的数据
  128. is = postMethod.getResponseBodyAsStream();
  129. // 封装输入流
  130. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  131. StringBuffer sbf = new StringBuffer();
  132. String temp = null;
  133. while ((temp = br.readLine()) != null) {
  134. sbf.append(temp).append("\r\n");
  135. }
  136. result = sbf.toString();
  137. } catch (IOException e) {
  138. e.printStackTrace();
  139. } finally {
  140. // 关闭资源
  141. if (null != br) {
  142. try {
  143. br.close();
  144. } catch (IOException e) {
  145. e.printStackTrace();
  146. }
  147. }
  148. if (null != is) {
  149. try {
  150. is.close();
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. }
  154. }
  155. // 释放连接
  156. postMethod.releaseConnection();
  157. }
  158. return result;
  159. }
  160. }