HttpClientUtils.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package com.ruoyi.common.utils.http;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import org.apache.http.client.config.RequestConfig;
  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.entity.StringEntity;
  8. import org.apache.http.impl.client.CloseableHttpClient;
  9. import org.apache.http.impl.client.HttpClients;
  10. import org.apache.http.message.BasicHeader;
  11. import org.apache.http.protocol.HTTP;
  12. import org.apache.http.util.EntityUtils;
  13. import java.io.IOException;
  14. import java.util.Map;
  15. public class HttpClientUtils {
  16. final static int TIMEOUT = 1000;
  17. final static int TIMEOUT_MSEC = 5 * 1000;
  18. public static String doPost(String url, JSONObject json) throws IOException {
  19. // 创建Httpclient对象
  20. CloseableHttpClient httpClient = HttpClients.createDefault();
  21. CloseableHttpResponse response = null;
  22. String resultString = "";
  23. try {
  24. // 创建Http Post请求
  25. HttpPost httpPost = new HttpPost(url);
  26. /* // 创建参数列表
  27. if (paramMap != null) {
  28. List<NameValuePair> paramList = new ArrayList<>();
  29. for (Map.Entry<String, String> param : paramMap.entrySet()) {
  30. paramList.add(new BasicNameValuePair(param.getKey(), param.getValue()));
  31. }
  32. // 模拟表单
  33. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
  34. httpPost.setEntity(entity);
  35. }*/
  36. StringEntity s = new StringEntity(json.toString(), "utf-8");
  37. s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
  38. "application/json"));
  39. httpPost.setEntity(s);
  40. httpPost.setConfig(builderRequestConfig());
  41. // 执行http请求
  42. response = httpClient.execute(httpPost);
  43. resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
  44. } catch (Exception e) {
  45. throw e;
  46. } finally {
  47. try {
  48. response.close();
  49. } catch (IOException e) {
  50. throw e;
  51. }
  52. }
  53. return resultString;
  54. }
  55. public static String doGet(String url, Map<String, String> paramMap) throws IOException {
  56. url += "?appid=" + paramMap.get("appid") + "&secret=" + paramMap.get("secret") + "&js_code=" + paramMap.get("js_code") + "&grant_type=" + paramMap.get("grant_type");
  57. // 创建Httpclient对象
  58. CloseableHttpClient httpClient = HttpClients.createDefault();
  59. CloseableHttpResponse response = null;
  60. String resultString = "";
  61. try {
  62. // 创建Http Post请求
  63. HttpGet httpGet = new HttpGet(url);
  64. // 执行http请求
  65. response = httpClient.execute(httpGet);
  66. resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
  67. } catch (Exception e) {
  68. throw e;
  69. } finally {
  70. try {
  71. response.close();
  72. } catch (IOException e) {
  73. throw e;
  74. }
  75. }
  76. return resultString;
  77. }
  78. public static String doGet1(String url) throws IOException {
  79. // 创建Httpclient对象
  80. CloseableHttpClient httpClient = HttpClients.createDefault();
  81. CloseableHttpResponse response = null;
  82. String resultString = "";
  83. try {
  84. // 创建Http Post请求
  85. HttpGet httpGet = new HttpGet(url);
  86. // 执行http请求
  87. response = httpClient.execute(httpGet);
  88. resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
  89. } catch (Exception e) {
  90. throw e;
  91. } finally {
  92. assert response != null;
  93. response.close();
  94. }
  95. return resultString;
  96. }
  97. private static RequestConfig builderRequestConfig() {
  98. return RequestConfig.custom()
  99. .setConnectTimeout(TIMEOUT_MSEC)
  100. .setConnectionRequestTimeout(TIMEOUT_MSEC)
  101. .setSocketTimeout(TIMEOUT_MSEC).build();
  102. }
  103. }