public class HttpClientUtil {
    private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    1. public static String doGet(String url, Map<String, String> param) {
    2. // 创建Httpclient对象
    3. CloseableHttpClient httpclient = HttpClients.createDefault();
    4. String resultString = "";
    5. CloseableHttpResponse response = null;
    6. try {
    7. // 创建uri
    8. URIBuilder builder = new URIBuilder(url);
    9. if (param != null) {
    10. for (String key : param.keySet()) {
    11. builder.addParameter(key, param.get(key));
    12. }
    13. }
    14. URI uri = builder.build();
    15. logger.debug("whole url:" + uri);
    16. // 创建http GET请求
    17. HttpGet httpGet = new HttpGet(uri);
    18. // 执行请求
    19. response = httpclient.execute(httpGet);
    20. // 判断返回状态是否为200
    21. if (response.getStatusLine().getStatusCode() == 200) {
    22. resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
    23. }
    24. } catch (Exception e) {
    25. e.printStackTrace();
    26. } finally {
    27. try {
    28. if (response != null) {
    29. response.close();
    30. }
    31. httpclient.close();
    32. } catch (IOException e) {
    33. e.printStackTrace();
    34. }
    35. }
    36. return resultString;
    37. }
    38. public static String doGet(String url) {
    39. return doGet(url, null);
    40. }
    41. /**
    42. * doPostByJson
    43. * @param url
    44. * @param json
    45. * @return
    46. */
    47. public static String doPostByJson(String url, String json, String chartSet) {
    48. String defalutChartSet = "UTF-8";
    49. if (chartSet != null) {
    50. defalutChartSet = chartSet;
    51. }
    52. // 创建Httpclient对象
    53. CloseableHttpClient httpClient = HttpClients.createDefault();
    54. CloseableHttpResponse response = null;
    55. String resultString = "";
    56. try {
    57. // 创建Http Post请求
    58. HttpPost httpPost = new HttpPost(url);
    59. // 创建请求内容
    60. StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
    61. entity.setContentEncoding(defalutChartSet);
    62. httpPost.setEntity(entity);
    63. // 执行http请求
    64. response = httpClient.execute(httpPost);
    65. resultString = EntityUtils.toString(response.getEntity(), defalutChartSet);
    66. } catch (Exception e) {
    67. e.printStackTrace();
    68. } finally {
    69. try {
    70. response.close();
    71. } catch (IOException e) {
    72. e.printStackTrace();
    73. }
    74. }
    75. return resultString;
    76. }
    77. /**
    78. *
    79. * @param url
    80. * @param param
    81. * @param chartSet
    82. * @return
    83. */
    84. public static String doPost(String url, Map<String, String> param, String chartSet) {
    85. String defalutChartSet = "UTF-8";
    86. if (chartSet != null) {
    87. defalutChartSet = chartSet;
    88. }
    89. // 创建Httpclient对象
    90. CloseableHttpClient httpClient = HttpClients.createDefault();
    91. CloseableHttpResponse response = null;
    92. String resultString = "";
    93. try {
    94. // 创建Http Post请求
    95. HttpPost httpPost = new HttpPost(url);
    96. // 创建参数列表
    97. if (param != null) {
    98. List<NameValuePair> paramList = new ArrayList<>();
    99. for (String key : param.keySet()) {
    100. paramList.add(new BasicNameValuePair(key, param.get(key)));
    101. }
    102. // 模拟表单
    103. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, defalutChartSet);
    104. httpPost.setEntity(entity);
    105. }
    106. // 执行http请求
    107. response = httpClient.execute(httpPost);
    108. resultString = EntityUtils.toString(response.getEntity(), defalutChartSet);
    109. } catch (Exception e) {
    110. e.printStackTrace();
    111. } finally {
    112. try {
    113. if (response != null) {
    114. response.close();
    115. }
    116. } catch (IOException e) {
    117. e.printStackTrace();
    118. }
    119. }
    120. return resultString;
    121. }
    122. public static String doPostByFile(String url, File file, String termSeq) {
    123. String resultString = "";
    124. CloseableHttpClient httpClient = null;
    125. CloseableHttpResponse response = null;
    126. try {
    127. httpClient = HttpClients.createDefault();
    128. // 把一个普通参数和文件上传给下面这个地址 是一个servlet
    129. HttpPost httpPost = new HttpPost(url);
    130. // 把文件转换成流对象FileBody
    131. FileBody bin = new FileBody(file);
    132. StringBody termSeqStr = new StringBody(termSeq, ContentType.create(
    133. "text/plain", Consts.UTF_8));
    134. HttpEntity reqEntity = MultipartEntityBuilder.create()
    135. // 相当于<input type="file" name="file"/>
    136. .addPart("file", bin)
    137. // 相当于<input type="text" name="userName" value=userName>
    138. .addPart("termSeq", termSeqStr)

    // .addPart(“pass”, password)
    .build();

    1. httpPost.setEntity(reqEntity);
    2. // 发起请求 并返回请求的响应
    3. response = httpClient.execute(httpPost);
    4. logger.info("The response value of token:" + response.getFirstHeader("token"));
    5. System.out.println("The response value of token:" + response.getFirstHeader("token"));
    6. // 获取响应对象
    7. HttpEntity resEntity = response.getEntity();
    8. if (resEntity != null) {
    9. // 打印响应长度
    10. logger.info("Response content length: " + resEntity.getContentLength());
    11. System.out.println("Response content length: " + resEntity.getContentLength());
    12. // 打印响应内容
    13. resultString = EntityUtils.toString(resEntity, Charset.forName("UTF-8"));
    14. }
    15. // 销毁
    16. EntityUtils.consume(resEntity);
    17. return resultString;
    18. } catch (Exception e) {
    19. e.printStackTrace();
    20. } finally {
    21. try {
    22. if (response != null) {
    23. response.close();
    24. }
    25. } catch (IOException e) {
    26. e.printStackTrace();
    27. }
    28. try {
    29. if (httpClient != null) {
    30. httpClient.close();
    31. }
    32. } catch (IOException e) {
    33. e.printStackTrace();
    34. }
    35. }
    36. return resultString;
    37. }
    38. /**
    39. *
    40. * @param url
    41. * @param param
    42. * @return
    43. */
    44. public static String doPost(String url, Map<String, String> param) {
    45. return doPost(url, param, null);
    46. }
    47. /**
    48. *
    49. * @param url
    50. * @return
    51. */
    52. public static String doPost(String url) {
    53. return doPost(url, null);
    54. }

    }