HttpUtils.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.template.common.utils;
  2. import org.apache.http.HttpEntity;
  3. import org.apache.http.NameValuePair;
  4. import org.apache.http.client.ClientProtocolException;
  5. import org.apache.http.client.config.RequestConfig;
  6. import org.apache.http.client.entity.UrlEncodedFormEntity;
  7. import org.apache.http.client.methods.CloseableHttpResponse;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.entity.StringEntity;
  10. import org.apache.http.impl.client.CloseableHttpClient;
  11. import org.apache.http.impl.client.HttpClients;
  12. import org.apache.http.message.BasicNameValuePair;
  13. import org.apache.http.util.EntityUtils;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.Map;
  18. /**
  19. * @Author: binguo
  20. * @Date: 2023/8/21 星期一 14:58
  21. * @Description: com.repair.common.utils
  22. * @Version: 1.0
  23. */
  24. public class HttpUtils {
  25. /**
  26. * 发送http post请求
  27. *
  28. * @param
  29. *
  30. * @param
  31. *
  32. */
  33. public static String post(String url, Map<String, String> params) throws IOException {
  34. CloseableHttpClient httpClient = null;
  35. HttpPost httpPost = null;
  36. String re = "";
  37. try {
  38. httpClient = HttpClients.createDefault();
  39. RequestConfig requestConfig = RequestConfig.custom()
  40. .setSocketTimeout(20000).setConnectTimeout(20000).build();
  41. httpPost = new HttpPost(url);
  42. httpPost.setConfig(requestConfig);
  43. List<NameValuePair> ps = new ArrayList<NameValuePair>();
  44. for (String pKey : params.keySet()) {
  45. ps.add(new BasicNameValuePair(pKey, params.get(pKey)));
  46. }
  47. httpPost.setEntity(new UrlEncodedFormEntity(ps));
  48. CloseableHttpResponse response = httpClient.execute(httpPost);
  49. HttpEntity httpEntity = response.getEntity();
  50. re = EntityUtils.toString(httpEntity, "utf-8");
  51. } catch (ClientProtocolException e) {
  52. e.printStackTrace();
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. } finally {
  56. try {
  57. if (httpPost != null) {
  58. httpPost.releaseConnection();
  59. }
  60. if (httpClient != null) {
  61. httpClient.close();
  62. }
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. return re;
  68. }
  69. /**
  70. * 发送post请求Https,参数是字符串
  71. *
  72. * @param
  73. * @return
  74. */
  75. public static String post(String url, String body) throws Exception {
  76. String str = "";
  77. CloseableHttpClient httpClient = null;
  78. HttpPost httpPost = null;
  79. try {
  80. httpClient = HttpClients.createDefault();
  81. RequestConfig requestConfig = RequestConfig.custom()
  82. .setSocketTimeout(20000).setConnectTimeout(20000).build();
  83. httpPost = new HttpPost(url);
  84. httpPost.setConfig(requestConfig);
  85. httpPost.setEntity(new StringEntity(body, "utf-8"));
  86. httpPost.setHeader("Content-Type", "application/json");
  87. httpPost.setHeader("Accept", "application/json");
  88. CloseableHttpResponse response = httpClient.execute(httpPost);
  89. HttpEntity httpEntity = response.getEntity();
  90. str = EntityUtils.toString(httpEntity, "utf-8");
  91. } catch (ClientProtocolException e) {
  92. e.printStackTrace();
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. } finally {
  96. try {
  97. if (httpPost != null) {
  98. httpPost.releaseConnection();
  99. }
  100. if (httpClient != null) {
  101. httpClient.close();
  102. }
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. return str;
  108. }
  109. /**
  110. * 判断User-Agent 是不是来自于手机
  111. * @param ua
  112. * @author Leemeea
  113. * @return
  114. */
  115. public static boolean checkAgentIsMobile(String ua) {
  116. String[] deviceArray = new String[] { "android", "iPhone", "ipod",
  117. "ipad", "blackberry", "ucweb", "windows phone" };
  118. if (ua == null) {
  119. return false;
  120. }
  121. ua = ua.toLowerCase();
  122. for (String string : deviceArray) {
  123. if (ua.indexOf(string) > 0) {
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. }