JAVA 使用httpclient发送常见HTTP POST 和 GET 请求

旧城等待, 2021-09-27 08:46 513阅读 0赞

JAVA项目开发中不可避免要发送http请求,http请求有get post请求,以下是自己整理的一个HTTP发送请求工具类。

maven pom文件配置

  1. <dependency>
  2. <groupId>commons-httpclient</groupId>
  3. <artifactId>commons-httpclient</artifactId>
  4. <version>3.1</version>
  5. </dependency>

HTTP工具类

  1. import org.apache.commons.httpclient.Header;
  2. import org.apache.commons.httpclient.HttpClient;
  3. import org.apache.commons.httpclient.HttpStatus;
  4. import org.apache.commons.httpclient.URI;
  5. import org.apache.commons.httpclient.methods.GetMethod;
  6. import org.apache.commons.httpclient.methods.PostMethod;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import java.io.IOException;
  10. import java.util.Map;
  11. /** * @author yunfeng * @version V1.0 * @date 2018/5/3 16:33 */
  12. public class HttpPostUtil {
  13. private final static Logger log = LoggerFactory.getLogger(HttpPostUtil.class);
  14. /** * @param url * @return */
  15. public static String getData(String url) {
  16. HttpClient client = new HttpClient();
  17. GetMethod method = new GetMethod(url);
  18. log.info("reqUrl: " + url);
  19. String respStr = "";
  20. try {
  21. int statusCode = client.executeMethod(method);
  22. log.info("Status Code = " + statusCode);
  23. respStr = method.getResponseBodyAsString();
  24. method.releaseConnection();
  25. } catch (IOException e) {
  26. log.error("发送HTTP GET请求失败!详情:" + e.toString());
  27. e.printStackTrace();
  28. }
  29. return respStr;
  30. }
  31. /** * @param url * @param jsonStr * @return */
  32. public static String postJson(String url, String jsonStr) {
  33. HttpClient client = new HttpClient();
  34. PostMethod method = new PostMethod(url);
  35. method.setRequestHeader("Content-Type", "application/json;charset=utf-8");
  36. String postBody = buildPostData(jsonStr);
  37. log.info("reqUrl: " + url);
  38. log.info("postBody:\r\n" + postBody);
  39. method.setRequestBody(postBody);
  40. String respStr = "";
  41. try {
  42. int statusCode = client.executeMethod(method);
  43. log.info("Status Code = " + statusCode);
  44. respStr = method.getResponseBodyAsString();
  45. // log.info("respStr:" + respStr);
  46. method.releaseConnection();
  47. } catch (IOException e) {
  48. log.error("发送HTTP POST JSON请求失败!详情:" + e.toString());
  49. e.printStackTrace();
  50. }
  51. return respStr;
  52. }
  53. /** * @param url * @param jsonStr * @return */
  54. public static String postJsonHeader(String url, Map<String, String> headerMap, String jsonStr) {
  55. HttpClient client = new HttpClient();
  56. PostMethod method = new PostMethod(url);
  57. method.setRequestHeader("Content-Type", "application/json;charset=utf-8");
  58. for (Map.Entry<String, String> entry : headerMap.entrySet()) {
  59. String key = entry.getKey();
  60. String val = entry.getValue();
  61. method.setRequestHeader(key, val);
  62. }
  63. String postBody = buildPostData(jsonStr);
  64. log.info("HTTP ReqUrl: " + url);
  65. log.info("HTTP ReqContent: Content-Type: application/json;charset=utf-8");
  66. log.info("HTTP PostBody:\r\n" + postBody);
  67. method.setRequestBody(postBody);
  68. String respStr = "";
  69. try {
  70. int statusCode = client.executeMethod(method);
  71. respStr = method.getResponseBodyAsString();
  72. if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
  73. Header header = method.getResponseHeader("Location");
  74. String location = "";
  75. if (header != null) {
  76. location = header.getValue();
  77. method.setURI(new URI(location));
  78. statusCode = client.executeMethod(method);
  79. respStr = method.getResponseBodyAsString();
  80. }
  81. }
  82. log.info("Status Code = " + statusCode);
  83. // log.info("respStr:" + respStr);
  84. method.releaseConnection();
  85. } catch (IOException e) {
  86. String detail = "发送HTTP POST JSON请求失败!详情:" + e.toString();
  87. log.error(detail, e);
  88. return null;
  89. }
  90. return respStr;
  91. }
  92. /** * @param url url * @param formStr form参数 * @return 请求返回 */
  93. public static String postForm(String url, String formStr) {
  94. log.info("post URL:" + url);
  95. HttpClient client = new HttpClient();
  96. PostMethod method = new PostMethod(url);
  97. method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  98. String postBody = buildPostData(formStr);
  99. log.info("postBody:\n" + postBody);
  100. method.setRequestBody(postBody);
  101. int statusCode = 0;
  102. String respStr = "";
  103. try {
  104. statusCode = client.executeMethod(method);
  105. log.info("http return status code = " + statusCode);
  106. respStr = method.getResponseBodyAsString();
  107. // log.info("http return respStr = " + respStr);
  108. } catch (IOException e) {
  109. log.error("发送http form请求失败!详情:" + e.toString());
  110. e.printStackTrace();
  111. }
  112. method.releaseConnection();
  113. return respStr;
  114. }
  115. /** * @param formStr form参数 * @return */
  116. private static String buildPostData(String formStr) {
  117. StringBuilder sb = new StringBuilder();
  118. sb.append(formStr);
  119. sb.append("\r\n");
  120. sb.append("\r\n");
  121. return sb.toString();
  122. }
  123. }

发表评论

表情:
评论列表 (有 0 条评论,513人围观)

还没有评论,来说两句吧...

相关阅读