1. public class HttpClientUtil {
    2. private final static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
    3. public static String doGet(String url, Map<String, String> param) {
    4. return doGet(url, param, "");
    5. }
    6. public static String doGet(String url, String body) {
    7. return doGet(url, null, body);
    8. }
    9. public static String doGet(String url) {
    10. return doGet(url, null,"");
    11. }
    12. @SuppressWarnings("deprecation")
    13. public static String doGet(String url, Map<String, String> param, String body) {
    14. CloseableHttpClient httpclient = null;
    15. String resultString = "";
    16. CloseableHttpResponse response = null;
    17. try {
    18. if(url.startsWith("https")){
    19. httpclient = getIgnoeSSLClient();
    20. } else {
    21. httpclient = HttpClients.createDefault();
    22. }
    23. // 创建uri
    24. URIBuilder builder = new URIBuilder(url);
    25. if (param != null) {
    26. for (String key : param.keySet()) {
    27. builder.addParameter(key, param.get(key));
    28. }
    29. } else if (StringUtils.isNotEmpty(body)) {
    30. builder.setQuery(body);
    31. }
    32. URI uri = builder.build();
    33. // 创建http GET请求
    34. HttpGet httpGet = new HttpGet(uri);
    35. RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
    36. httpGet.setConfig(requestConfig);
    37. // 执行请求
    38. response = httpclient.execute(httpGet);
    39. // 判断返回状态是否为200
    40. if (response.getStatusLine().getStatusCode() == 200) {
    41. resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
    42. }
    43. if(url.startsWith("https")){
    44. EntityUtils.consume(response.getEntity());
    45. }
    46. } catch (Exception e) {
    47. logger.error("do get error.", e);
    48. return resultString;
    49. } finally {
    50. try {
    51. if (response != null) {
    52. response.close();
    53. }
    54. if(httpclient != null) {
    55. httpclient.close();
    56. }
    57. } catch (IOException e) {
    58. logger.error("do get error.", e);
    59. }
    60. }
    61. return resultString;
    62. }
    63. public static String doPost(String url, Map<String, String> param) {
    64. return doPost(url, param, "");
    65. }
    66. public static String doPost(String url, String body) {
    67. return doPost(url, null, body);
    68. }
    69. public static String doPost(String url, Map<String, String> param, String body) {
    70. CloseableHttpClient httpClient = null;
    71. CloseableHttpResponse response = null;
    72. String resultString = "";
    73. try {
    74. // 创建Httpclient对象
    75. if(url.startsWith("https")){
    76. httpClient = getIgnoeSSLClient();
    77. } else {
    78. httpClient = HttpClients.createDefault();
    79. }
    80. // 创建Http Post请求
    81. HttpPost httpPost = new HttpPost(url);
    82. RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
    83. httpPost.setConfig(requestConfig);
    84. // 创建参数列表
    85. if (param != null) {
    86. List<NameValuePair> paramList = new ArrayList<>();
    87. for (String key : param.keySet()) {
    88. paramList.add(new BasicNameValuePair(key, param.get(key)));
    89. }
    90. // 模拟表单
    91. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
    92. httpPost.setEntity(entity);
    93. } else if (StringUtils.isNotEmpty(body)) {
    94. StringEntity entity = null;
    95. try {
    96. entity = new StringEntity(body, "UTF-8");
    97. } catch (UnsupportedCharsetException ex) {
    98. ex.printStackTrace();
    99. }
    100. httpPost.setEntity(entity);
    101. // post.setRequestBody(body);
    102. }
    103. // 执行http请求
    104. response = httpClient.execute(httpPost);
    105. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
    106. if(url.startsWith("https")){
    107. EntityUtils.consume(response.getEntity());
    108. }
    109. } catch (Exception e) {
    110. logger.error("do post error.", e);
    111. return resultString;
    112. } finally {
    113. try {
    114. if(response != null) {
    115. response.close();
    116. }
    117. if(httpClient != null) {
    118. httpClient.close();
    119. }
    120. } catch (IOException e) {
    121. e.printStackTrace();
    122. }
    123. }
    124. return resultString;
    125. }
    126. public static String doPost(String url) {
    127. return doPost(url, null,"");
    128. }
    129. public static String doPost(String url,Map<String, String> head, Map<String, String> bodyParam) {
    130. CloseableHttpClient httpClient = null;
    131. CloseableHttpResponse response = null;
    132. String resultString = "";
    133. try {
    134. // 创建Httpclient对象
    135. if(url.startsWith("https")){
    136. httpClient = getIgnoeSSLClient();
    137. } else {
    138. httpClient = HttpClients.createDefault();
    139. }
    140. // 创建Http Post请求
    141. HttpPost httpPost = new HttpPost(url);
    142. RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
    143. httpPost.setConfig(requestConfig);
    144. // 创建参数列表
    145. if (head != null) {
    146. for (String key : head.keySet()) {
    147. httpPost.setHeader(key, head.get(key));
    148. }
    149. }
    150. if (bodyParam != null) {
    151. List<NameValuePair> paramList = new ArrayList<>();
    152. for (String key : bodyParam.keySet()) {
    153. paramList.add(new BasicNameValuePair(key, bodyParam.get(key)));
    154. }
    155. // 模拟表单
    156. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
    157. httpPost.setEntity(entity);
    158. }
    159. // 执行http请求
    160. response = httpClient.execute(httpPost);
    161. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
    162. if(url.startsWith("https")){
    163. EntityUtils.consume(response.getEntity());
    164. }
    165. } catch (Exception e) {
    166. logger.error("do post error.", e);
    167. return resultString;
    168. } finally {
    169. try {
    170. if(response != null) {
    171. response.close();
    172. }
    173. if(httpClient != null) {
    174. httpClient.close();
    175. }
    176. } catch (IOException e) {
    177. e.printStackTrace();
    178. }
    179. }
    180. return resultString;
    181. }
    182. public static String doPostJson(String url, String json) {
    183. CloseableHttpClient httpClient = null;
    184. CloseableHttpResponse response = null;
    185. String resultString = "";
    186. try {
    187. // 创建Httpclient对象
    188. if(url.startsWith("https")){
    189. httpClient = getIgnoeSSLClient();
    190. } else {
    191. httpClient = HttpClients.createDefault();
    192. }
    193. HttpPost httpPost = new HttpPost(url);
    194. // 创建请求内容
    195. StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
    196. httpPost.setEntity(entity);
    197. RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
    198. httpPost.setConfig(requestConfig);
    199. // 执行http请求
    200. response = httpClient.execute(httpPost);
    201. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
    202. if(url.startsWith("https")){
    203. EntityUtils.consume(response.getEntity());
    204. }
    205. } catch (Exception e) {
    206. logger.error("do post error.", e);
    207. return resultString;
    208. } finally {
    209. try {
    210. if (response != null) {
    211. response.close();
    212. }
    213. if(httpClient != null) {
    214. httpClient.close();
    215. }
    216. } catch (IOException e) {
    217. e.printStackTrace();
    218. }
    219. }
    220. return resultString;
    221. }
    222. /**
    223. * http发送文件
    224. * @param url
    225. * @param localFilePath
    226. * @return
    227. */
    228. public static String doPostFile(String url, String localFilePath) {
    229. CloseableHttpClient httpClient = null;
    230. CloseableHttpResponse response = null;
    231. String resultString = "";
    232. try {
    233. // 创建Httpclient对象
    234. if(url.startsWith("https")){
    235. httpClient = getIgnoeSSLClient();
    236. } else {
    237. httpClient = HttpClients.createDefault();
    238. }
    239. // 创建Http Post请求
    240. HttpPost httpPost = new HttpPost(url);
    241. // 创建请求内容
    242. // 把文件转换成流对象FileBody
    243. File localFile = new File(localFilePath);
    244. FileBody fileBody = new FileBody(localFile);
    245. // 以浏览器兼容模式运行,防止文件名乱码。
    246. HttpEntity entity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).
    247. addPart("uploadFile", fileBody).setCharset(CharsetUtils.get("UTF-8")).build();
    248. httpPost.setEntity(entity);
    249. // 执行http请求
    250. response = httpClient.execute(httpPost);
    251. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
    252. if(url.startsWith("https")){
    253. EntityUtils.consume(response.getEntity());
    254. }
    255. } catch (Exception e) {
    256. logger.error("do post error.", e);
    257. return resultString;
    258. } finally {
    259. try {
    260. if (response != null) {
    261. response.close();
    262. }
    263. if(httpClient != null) {
    264. httpClient.close();
    265. }
    266. } catch (IOException e) {
    267. e.printStackTrace();
    268. }
    269. }
    270. return resultString;
    271. }
    272. public static String doPut(String url, String json) {
    273. CloseableHttpClient httpClient = null;
    274. CloseableHttpResponse response = null;
    275. String resultString = "";
    276. try {
    277. // 创建Httpclient对象
    278. if(url.startsWith("https")){
    279. httpClient = getIgnoeSSLClient();
    280. } else {
    281. httpClient = HttpClients.createDefault();
    282. }
    283. HttpPut httpPut = new HttpPut(url);
    284. // 创建请求内容
    285. StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
    286. httpPut.setEntity(entity);
    287. // //设置请求超时时间
    288. // RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
    289. // httpPut.setConfig(requestConfig);
    290. // 执行http请求
    291. response = httpClient.execute(httpPut);
    292. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
    293. if(url.startsWith("https")){
    294. EntityUtils.consume(response.getEntity());
    295. }
    296. } catch (Exception e) {
    297. logger.error("do put error.", e);
    298. return resultString;
    299. } finally {
    300. try {
    301. if (response != null) {
    302. response.close();
    303. }
    304. if(httpClient != null) {
    305. httpClient.close();
    306. }
    307. } catch (IOException e) {
    308. e.printStackTrace();
    309. }
    310. }
    311. return resultString;
    312. }
    313. private static CloseableHttpClient getIgnoeSSLClient() throws Exception {
    314. SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
    315. @Override
    316. public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    317. return true;
    318. }
    319. }).build();
    320. CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext).
    321. setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
    322. return client;
    323. }
    324. }