HttpClientUtil.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.simuwang.daq.util;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.huawei.shade.org.apache.http.HttpResponse;
  4. import com.huawei.shade.org.apache.http.client.methods.HttpGet;
  5. import com.huawei.shade.org.apache.http.client.methods.HttpPost;
  6. import com.huawei.shade.org.apache.http.client.methods.HttpRequestBase;
  7. import com.huawei.shade.org.apache.http.entity.StringEntity;
  8. import com.huawei.shade.org.apache.http.impl.client.CloseableHttpClient;
  9. import com.huawei.shade.org.apache.http.impl.client.HttpClients;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import java.io.ByteArrayOutputStream;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.io.UnsupportedEncodingException;
  16. import java.nio.charset.StandardCharsets;
  17. import java.util.Iterator;
  18. import java.util.Map;
  19. import java.util.Set;
  20. public class HttpClientUtil {
  21. static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
  22. public static String get(String url, Map<String, String> header) {
  23. HttpGet httpget = new HttpGet(url);
  24. addHeader(httpget, header);
  25. return getResult(httpget);
  26. }
  27. public static String get(String url, Map<String, String> header, String charsets) {
  28. HttpGet httpget = new HttpGet(url);
  29. header.put("EMAIL-PARSE-TOKEN", "EMAIL-PARSE-TOKEN");
  30. addHeader(httpget, header);
  31. return getResult(httpget, charsets);
  32. }
  33. private static HttpPost getPost(String url, Map<String, String> header, String json) {
  34. HttpPost httpPost = new HttpPost(url);
  35. if (StrUtil.isNotBlank(json)) {
  36. StringEntity entity = getEntity(json, StandardCharsets.UTF_8.name(), "application/json; charset=utf-8");
  37. httpPost.setEntity(entity);
  38. }
  39. addHeader(httpPost, header);
  40. return httpPost;
  41. }
  42. public static String post(String url, Map<String, String> header, String json) {
  43. // header.put("EMAIL-PARSE-TOKEN", "EMAIL-PARSE-TOKEN");
  44. HttpPost httpPost = getPost(url, header, json);
  45. return getResult(httpPost);
  46. }
  47. private static StringEntity getEntity(String content, String charset, String contentType) {
  48. StringEntity entity = new StringEntity(content, charset);
  49. entity.setContentEncoding(charset);
  50. entity.setContentType(contentType);
  51. return entity;
  52. }
  53. private static void addHeader(HttpRequestBase httpType, Map<String, String> header) {
  54. if (header != null && header.size() > 0) {
  55. Set<String> headerKey = header.keySet();
  56. Iterator var4 = headerKey.iterator();
  57. while (var4.hasNext()) {
  58. String key = (String) var4.next();
  59. httpType.setHeader(key, (String) header.get(key));
  60. }
  61. }
  62. }
  63. public static String getResult(HttpRequestBase httpType) {
  64. return getResult(httpType, StandardCharsets.UTF_8.name());
  65. }
  66. public static String getResult(HttpRequestBase httpType, String charsets) {
  67. CloseableHttpClient httpClient = null;
  68. String var8;
  69. try {
  70. httpClient = HttpClients.createDefault();
  71. HttpResponse response = httpClient.execute(httpType);
  72. int status = response.getStatusLine().getStatusCode();
  73. if (status != 200) {
  74. logger.error("请求失败url:{},错误的HTTP Status:{}", httpType.getURI().toString(), status);
  75. return null;
  76. }
  77. InputStream is = response.getEntity().getContent();
  78. String retString = InputStreamTOString(is, charsets);
  79. var8 = retString;
  80. } catch (Exception var17) {
  81. logger.error(httpType.getURI().toString(), var17);
  82. return null;
  83. } finally {
  84. try {
  85. if (httpClient != null) {
  86. httpClient.close();
  87. }
  88. } catch (IOException var16) {
  89. var16.printStackTrace();
  90. }
  91. }
  92. return var8;
  93. }
  94. private static String InputStreamTOString(InputStream in, String encoding) {
  95. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  96. byte[] data = new byte[4096];
  97. boolean var4 = true;
  98. try {
  99. int count;
  100. while ((count = in.read(data, 0, 4096)) != -1) {
  101. outStream.write(data, 0, count);
  102. }
  103. String result = new String(outStream.toByteArray(), encoding);
  104. in.close();
  105. outStream.flush();
  106. outStream.close();
  107. return result;
  108. } catch (UnsupportedEncodingException var6) {
  109. var6.printStackTrace();
  110. return null;
  111. } catch (IOException var7) {
  112. var7.printStackTrace();
  113. return null;
  114. }
  115. }
  116. }