POM

  1. <!-- Apache Http Begin -->
  2. <dependency>
  3. <groupId>org.apache.httpcomponents</groupId>
  4. <artifactId>httpclient</artifactId>
  5. <version>4.5.5</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.httpcomponents</groupId>
  9. <artifactId>fluent-hc</artifactId>
  10. <version>4.5.5</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.httpcomponents</groupId>
  14. <artifactId>httpmime</artifactId>
  15. <version>4.5.5</version>
  16. </dependency>
  17. <!-- Apache Http End -->

工具类

  1. package cn.hzlim.hello.httpclient;
  2. import org.apache.http.HttpEntity;
  3. import org.apache.http.client.entity.UrlEncodedFormEntity;
  4. import org.apache.http.client.methods.CloseableHttpResponse;
  5. import org.apache.http.client.methods.HttpGet;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.impl.client.CloseableHttpClient;
  8. import org.apache.http.impl.client.HttpClients;
  9. import org.apache.http.message.BasicNameValuePair;
  10. import org.apache.http.util.EntityUtils;
  11. import java.io.IOException;
  12. import java.util.Arrays;
  13. /**
  14. *
  15. * HttpClient工具类
  16. * @title: HttpClientUtils
  17. * @description:
  18. *
  19. * @author: xianyu_tf
  20. * @date: 2019-07-12 22:53
  21. * @version 1.0.0
  22. *
  23. */
  24. public class HttpClientUtils {
  25. public static final String GET="get";
  26. public static final String POST="post";
  27. public static final String REQUEST_HEADER_CONNECTION="keep-alive";
  28. public static final String REQUEST_HEADER_USER_AGENT="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36";
  29. /**
  30. * get请求
  31. * @param url 请求地址
  32. * @return
  33. */
  34. public static String doGet(String url){
  35. return createRequest(url,GET,null);
  36. }
  37. /**
  38. * get请求
  39. * @param url 请求地址
  40. * @param cookie cookie
  41. * @return
  42. */
  43. public static String doGet(String url,String cookie){
  44. return createRequest(url,GET,cookie);
  45. }
  46. /**
  47. * post请求
  48. * @param url 请求地址
  49. * @param params 请求参数
  50. * @return
  51. */
  52. public static String doPost(String url,BasicNameValuePair... params){
  53. return createRequest(url,POST,null,params);
  54. }
  55. /**
  56. * post请求
  57. * @param url 请求地址
  58. * @param cookie cookie
  59. * @param params 请求参数
  60. * @return
  61. */
  62. public static String doPost(String url,String cookie,BasicNameValuePair... params){
  63. return createRequest(url,POST,cookie,params);
  64. }
  65. /**
  66. * 创建请求
  67. * @param url 请求地址
  68. * @param requestMethod 请求方式get/post
  69. * @param cookie cookie
  70. * @param params 请求参数 仅限于post
  71. * @return
  72. */
  73. private static String createRequest(String url,String requestMethod,String cookie,BasicNameValuePair... params){
  74. CloseableHttpClient httpClient = HttpClients.createDefault();
  75. String result = null;
  76. //请求方式
  77. HttpPost httpPost = null;
  78. HttpGet httpGet = null;
  79. //响应
  80. CloseableHttpResponse httpResponse = null;
  81. try {
  82. if (GET.equals(requestMethod)){
  83. httpGet = new HttpGet(url);
  84. httpGet.setHeader("Connection", REQUEST_HEADER_CONNECTION);
  85. httpGet.setHeader("Cookie", cookie);
  86. httpGet.setHeader("User-Agent", REQUEST_HEADER_USER_AGENT);
  87. httpResponse = httpClient.execute(httpGet);
  88. }else if(POST.equals(requestMethod)){
  89. httpPost = new HttpPost(url);
  90. httpPost.setHeader("Connection", REQUEST_HEADER_CONNECTION);
  91. httpPost.setHeader("Cookie", cookie);
  92. httpPost.setHeader("User-Agent", REQUEST_HEADER_USER_AGENT);
  93. //判断是否有参数
  94. if(params != null && params.length > 0){
  95. httpPost.setEntity(new UrlEncodedFormEntity(Arrays.asList(params), "UTF-8"));
  96. }
  97. httpResponse = httpClient.execute(httpPost);
  98. }
  99. HttpEntity httpEntity = httpResponse.getEntity();
  100. result = EntityUtils.toString(httpEntity);
  101. }catch (IOException e){
  102. e.printStackTrace();
  103. }finally {
  104. //关闭连接
  105. if (httpResponse != null) {
  106. try {
  107. httpResponse.close();
  108. } catch (IOException e) {
  109. e.printStackTrace();
  110. }
  111. }
  112. if (httpClient != null) {
  113. try {
  114. httpClient.close();
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. }
  118. }
  119. }
  120. return result;
  121. }
  122. }