PostUtil.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package cn.com.lzt.common.util;
  2. import java.util.Map;
  3. import java.util.List;
  4. import java.util.Iterator;
  5. import java.util.ArrayList;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.nio.charset.Charset;
  9. import java.io.ByteArrayOutputStream;
  10. import org.apache.http.HttpEntity;
  11. import org.apache.http.HttpHeaders;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.util.EntityUtils;
  14. import org.apache.http.entity.StringEntity;
  15. import org.apache.http.impl.client.HttpClients;
  16. import org.apache.http.client.methods.HttpPost;
  17. import org.apache.http.message.BasicNameValuePair;
  18. import org.apache.http.client.methods.HttpUriRequest;
  19. import org.apache.http.impl.client.HttpClientBuilder;
  20. import org.apache.http.impl.client.CloseableHttpClient;
  21. import org.apache.http.client.entity.UrlEncodedFormEntity;
  22. import org.apache.http.client.methods.CloseableHttpResponse;
  23. /**
  24. * Post请求工具类
  25. */
  26. public class PostUtil {
  27. public static final String UTF8 = "utf-8";
  28. private static final String CONTENT_TYPE = "application/json";
  29. public static String send(String url, Map<String, String> headers, Object params) {
  30. try {
  31. if (params instanceof Map)
  32. return sendMap(url, headers, (Map<String, Object>)params);
  33. else
  34. return sendJson(url, headers, (String)params);
  35. } catch (Exception e){
  36. return null;
  37. }
  38. }
  39. public static String sendMap(String url, Map<String, String> headers, Map<String, Object> params) throws IOException {
  40. List<BasicNameValuePair> pairs = new ArrayList<>();
  41. for (Iterator<String> iterator = params.keySet().iterator(); iterator.hasNext();) {
  42. String name = iterator.next();
  43. Object value = params.get(name);
  44. pairs.add(new BasicNameValuePair(name, String.valueOf(value)));
  45. }
  46. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, UTF8);
  47. return doAction(url, headers, entity);
  48. }
  49. public static String sendJson(String url, Map<String, String> headers, String params) throws IOException {
  50. StringEntity entity = new StringEntity(params, UTF8);
  51. return doAction(url, headers, entity);
  52. }
  53. private static String doAction(String url, Map<String, String> headers, StringEntity body) throws IOException {
  54. HttpPost post = new HttpPost(url);
  55. post.setEntity(body);
  56. for (String key : headers.keySet())
  57. post.addHeader(key, headers.get(key));
  58. CloseableHttpClient client = HttpClients.createDefault();
  59. CloseableHttpResponse response = client.execute(post);
  60. HttpEntity entity = response.getEntity();
  61. String result = null;
  62. if (entity != null)
  63. result = EntityUtils.toString(entity, UTF8);
  64. response.close();
  65. return result;
  66. }
  67. /**
  68. * 发送 POST JSON 请求
  69. */
  70. public static String postJson(String url, String data) {
  71. HttpClientBuilder builder = HttpClientBuilder.create();
  72. HttpUriRequest request = null;
  73. // POST请求
  74. HttpEntity entity = new StringEntity(data, UTF8);
  75. HttpPost post = new HttpPost(url);
  76. post.setEntity(entity);
  77. request = post;
  78. request.setHeader(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE);
  79. try (CloseableHttpClient closeableHttpClient = builder.build()) {
  80. HttpResponse resp = closeableHttpClient.execute(request);
  81. InputStream respIs = resp.getEntity().getContent();
  82. byte[] respBytes = toByteArray(respIs);
  83. String result = new String(respBytes, Charset.forName(UTF8));
  84. return result;
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. return null;
  89. }
  90. private static byte[] toByteArray(InputStream is) throws IOException {
  91. ByteArrayOutputStream output = new ByteArrayOutputStream();
  92. try {
  93. byte[] b = new byte[4096];
  94. boolean var3 = false;
  95. int n;
  96. while ((n = is.read(b)) != -1) {
  97. output.write(b, 0, n);
  98. }
  99. byte[] var4 = output.toByteArray();
  100. return var4;
  101. } finally {
  102. output.close();
  103. }
  104. }
  105. }