HttpRequestUtil.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.kmall.api.util;
  2. import java.io.IOException;
  3. import java.util.Map;
  4. import org.apache.commons.httpclient.HttpClient;
  5. import org.apache.commons.httpclient.HttpException;
  6. import org.apache.commons.httpclient.SimpleHttpConnectionManager;
  7. import org.apache.commons.httpclient.methods.GetMethod;
  8. import org.apache.commons.httpclient.methods.PostMethod;
  9. /**
  10. * @author huangyaqin
  11. * @version 1.0
  12. * 2018-10-16 11:28
  13. */
  14. public class HttpRequestUtil {
  15. /**
  16. * HttpClient 模拟POST请求
  17. * @param url
  18. * @param params
  19. * @return
  20. */
  21. public static String postRequest(String url, Map<String, String> params) {
  22. //构造HttpClient的实例
  23. HttpClient httpClient = new HttpClient();
  24. //创建POST方法的实例
  25. PostMethod postMethod = new PostMethod(url);
  26. //设置请求头信息
  27. postMethod.setRequestHeader("Connection", "close");
  28. //添加参数
  29. for (Map.Entry<String, String> entry : params.entrySet()) {
  30. postMethod.addParameter(entry.getKey(), entry.getValue());
  31. }
  32. //使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次
  33. httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
  34. //接收处理结果
  35. String result = null;
  36. try {
  37. //执行Http Post请求
  38. httpClient.executeMethod(postMethod);
  39. //返回处理结果
  40. result = postMethod.getResponseBodyAsString();
  41. } catch (HttpException e) {
  42. // 发生致命的异常,可能是协议不对或者返回的内容有问题
  43. System.out.println("请检查输入的URL!");
  44. e.printStackTrace();
  45. } catch (IOException e) {
  46. // 发生网络异常
  47. System.out.println("发生网络异常!");
  48. e.printStackTrace();
  49. } finally {
  50. //释放链接
  51. postMethod.releaseConnection();
  52. //关闭HttpClient实例
  53. if (httpClient != null) {
  54. ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
  55. httpClient = null;
  56. }
  57. }
  58. return result;
  59. }
  60. /**
  61. * HttpClient 模拟GET请求
  62. * 方法说明
  63. * @Discription:扩展说明
  64. * @param url
  65. * @param params
  66. * @return String
  67. */
  68. public static String getRequest(String url, Map<String, String> params) {
  69. //构造HttpClient实例
  70. HttpClient client = new HttpClient();
  71. //拼接参数
  72. String paramStr = "";
  73. for (String key : params.keySet()) {
  74. paramStr = paramStr + "&" + key + "=" + params.get(key);
  75. }
  76. paramStr = paramStr.substring(1);
  77. //创建GET方法的实例
  78. GetMethod method = new GetMethod(url + "?" + paramStr);
  79. //接收返回结果
  80. String result = null;
  81. try {
  82. //执行HTTP GET方法请求
  83. client.executeMethod(method);
  84. //返回处理结果
  85. result = method.getResponseBodyAsString();
  86. } catch (HttpException e) {
  87. // 发生致命的异常,可能是协议不对或者返回的内容有问题
  88. System.out.println("请检查输入的URL!");
  89. e.printStackTrace();
  90. } catch (IOException e) {
  91. // 发生网络异常
  92. System.out.println("发生网络异常!");
  93. e.printStackTrace();
  94. } finally {
  95. //释放链接
  96. method.releaseConnection();
  97. //关闭HttpClient实例
  98. if (client != null) {
  99. ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
  100. client = null;
  101. }
  102. }
  103. return result;
  104. }
  105. }