HttpClientUtil.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.platform.yijia.utils;
  2. import org.apache.http.NameValuePair;
  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.client.utils.URIBuilder;
  8. import org.apache.http.entity.ContentType;
  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.net.URI;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.Map;
  19. /**
  20. * @Author : suxinghua
  21. * @CreateTime : 2020/11/24
  22. * @Description :
  23. **/
  24. public class HttpClientUtil {
  25. public static String doGet(String url, Map<String, String> param) {
  26. // 创建Httpclient对象
  27. CloseableHttpClient httpclient = HttpClients.createDefault();
  28. String resultString = "";
  29. CloseableHttpResponse response = null;
  30. try {
  31. // 创建uri
  32. URIBuilder builder = new URIBuilder(url);
  33. if (param != null) {
  34. for (String key : param.keySet()) {
  35. builder.addParameter(key, param.get(key));
  36. }
  37. }
  38. URI uri = builder.build();
  39. // 创建http GET请求
  40. HttpGet httpGet = new HttpGet(uri);
  41. // 执行请求
  42. response = httpclient.execute(httpGet);
  43. // 判断返回状态是否为200
  44. if (response.getStatusLine().getStatusCode() == 200) {
  45. resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
  46. }
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. } finally {
  50. try {
  51. if (response != null) {
  52. response.close();
  53. }
  54. httpclient.close();
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. return resultString;
  60. }
  61. public static String doGet(String url) {
  62. return doGet(url, null);
  63. }
  64. public static String doPost(String url, Map<String, String> param) {
  65. // 创建Httpclient对象
  66. CloseableHttpClient httpClient = HttpClients.createDefault();
  67. CloseableHttpResponse response = null;
  68. String resultString = "";
  69. try {
  70. // 创建Http Post请求
  71. HttpPost httpPost = new HttpPost(url);
  72. // 创建参数列表
  73. if (param != null) {
  74. List<NameValuePair> paramList = new ArrayList<>();
  75. for (String key : param.keySet()) {
  76. paramList.add(new BasicNameValuePair(key, param.get(key)));
  77. }
  78. // 模拟表单
  79. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
  80. httpPost.setEntity(entity);
  81. }
  82. // 执行http请求
  83. response = httpClient.execute(httpPost);
  84. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. } finally {
  88. try {
  89. response.close();
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. return resultString;
  95. }
  96. public static String doPost(String url) {
  97. return doPost(url, null);
  98. }
  99. public static String doPostJson(String url, String json) {
  100. // 创建Httpclient对象
  101. CloseableHttpClient httpClient = HttpClients.createDefault();
  102. CloseableHttpResponse response = null;
  103. String resultString = "";
  104. try {
  105. // 创建Http Post请求
  106. HttpPost httpPost = new HttpPost(url);
  107. // 创建请求内容
  108. StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
  109. httpPost.setEntity(entity);
  110. // 执行http请求
  111. response = httpClient.execute(httpPost);
  112. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. } finally {
  116. try {
  117. response.close();
  118. } catch (IOException e) {
  119. e.printStackTrace();
  120. }
  121. }
  122. return resultString;
  123. }
  124. }