1. import org.apache.http.Consts;
    2. import org.apache.http.HttpEntity;
    3. import org.apache.http.HttpResponse;
    4. import org.apache.http.NameValuePair;
    5. import org.apache.http.StatusLine;
    6. import org.apache.http.client.HttpResponseException;
    7. import org.apache.http.client.fluent.Request;
    8. import org.apache.http.client.methods.CloseableHttpResponse;
    9. import org.apache.http.client.methods.HttpGet;
    10. import org.apache.http.client.utils.URIBuilder;
    11. import org.apache.http.entity.ContentType;
    12. import org.apache.http.impl.client.BasicResponseHandler;
    13. import org.apache.http.impl.client.CloseableHttpClient;
    14. import org.apache.http.impl.client.HttpClients;
    15. import org.apache.http.message.BasicNameValuePair;
    16. import org.apache.http.util.EntityUtils;
    17. import java.io.IOException;
    18. import java.net.URI;
    19. import java.util.ArrayList;
    20. import java.util.List;
    21. import java.util.Map;
    22. public class FluentHttpUtil {
    23. /**
    24. * 连接超时: 5秒
    25. */
    26. public static final int CONNECT_TIMEOUT = 5 * 1000;
    27. /**
    28. * socket超时: 1分钟
    29. */
    30. public static final int SOCKET_TIMEOUT = 60 * 1000;
    31. /**
    32. * 发送GET请求
    33. *
    34. * @param url
    35. * @return
    36. * @throws Exception
    37. */
    38. public static String sendGetRequest(String url) throws Exception {
    39. return Request.Get(url)
    40. .useExpectContinue()
    41. .connectTimeout(CONNECT_TIMEOUT) // 连接超时: 5秒
    42. .socketTimeout(SOCKET_TIMEOUT) // socket超时: 1分钟
    43. .execute()
    44. .returnContent()
    45. .asString();
    46. }
    47. /**
    48. * 发送GET请求
    49. *
    50. * @param url
    51. * @param paramMap
    52. * @return
    53. * @throws Exception
    54. */
    55. public static String sendGetRequest(String url, Map<String, Object> paramMap) throws Exception {
    56. /*
    57. * 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
    58. */
    59. URIBuilder uriBuilder = new URIBuilder(url);
    60. // 1、创建httpClient
    61. CloseableHttpClient client = HttpClients.createDefault();
    62. // 2、封装请求参数
    63. List<NameValuePair> list = new ArrayList<>();
    64. paramMap.forEach((k, v) -> {
    65. list.add(new BasicNameValuePair(k, v.toString()));
    66. });
    67. // 3、转化参数
    68. // String params = EntityUtils.toString(new UrlEncodedFormEntity(list, Consts.UTF_8));
    69. // System.out.println(params);
    70. // 设置URL请求参数
    71. uriBuilder.setParameters(list);
    72. // 构建请求URL对象
    73. URI uri = uriBuilder.build();
    74. // 4、创建HttpGet请求
    75. HttpGet httpGet = new HttpGet(uri);
    76. CloseableHttpResponse response = client.execute(httpGet);
    77. // 5、获取实体
    78. HttpEntity entity = response.getEntity();
    79. // 将实体装成字符串
    80. String result = EntityUtils.toString(entity, Consts.UTF_8.name());
    81. response.close();
    82. return result;
    83. }
    84. /**
    85. * 发送POST请求,参数为JSON格式
    86. *
    87. * @param url
    88. * @param param
    89. * @return
    90. * @throws Exception
    91. */
    92. public static String sendPostJson(String url, String param) throws Exception {
    93. return Request.Post(url)
    94. .useExpectContinue()
    95. .connectTimeout(CONNECT_TIMEOUT) // 连接超时: 5秒
    96. .socketTimeout(SOCKET_TIMEOUT) // socket超时: 1分钟
    97. .bodyString(param, ContentType.APPLICATION_JSON)
    98. .execute()
    99. .returnContent()
    100. .asString();
    101. }
    102. /**
    103. * 发送POST请求,参数为JSON格式
    104. *
    105. * @param url
    106. * @param param
    107. * @param returnBodyOnError
    108. * 响应状态码大于300时,是否返回body. true表示返回, false表示抛出异常
    109. * @return
    110. * @throws Exception
    111. */
    112. public static String sendPostJson(String url, String param, boolean returnBodyOnError) throws Exception {
    113. return Request.Post(url)
    114. .useExpectContinue()
    115. .connectTimeout(CONNECT_TIMEOUT) // 连接超时: 5秒
    116. .socketTimeout(SOCKET_TIMEOUT) // socket超时: 1分钟
    117. .bodyString(param, ContentType.APPLICATION_JSON)
    118. .execute()
    119. .handleResponse(new BasicResponseHandler() {
    120. @Override
    121. public String handleResponse(HttpResponse response) throws HttpResponseException, IOException {
    122. final StatusLine statusLine = response.getStatusLine();
    123. final HttpEntity entity = response.getEntity();
    124. if (!returnBodyOnError && statusLine.getStatusCode() >= 300) {
    125. EntityUtils.consume(entity);
    126. throw new HttpResponseException(statusLine.getStatusCode(),
    127. statusLine.getReasonPhrase());
    128. }
    129. return entity == null ? null : handleEntity(entity);
    130. }
    131. });
    132. }
    133. /**
    134. * 发送PUT请求,参数为JSON格式
    135. *
    136. * @param url
    137. * @param param
    138. * @return
    139. * @throws Exception
    140. */
    141. public static String sendPutJson(String url, String param) throws Exception {
    142. return Request.Put(url)
    143. .useExpectContinue()
    144. .connectTimeout(CONNECT_TIMEOUT) // 连接超时: 5秒
    145. .socketTimeout(SOCKET_TIMEOUT) // socket超时: 1分钟
    146. .bodyString(param, ContentType.APPLICATION_JSON)
    147. .execute()
    148. .returnContent()
    149. .asString();
    150. }
    151. }
    1. <dependency>
    2. <groupId>org.apache.httpcomponents</groupId>
    3. <artifactId>fluent-hc</artifactId>
    4. </dependency>