HttpHelper.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package cn.com.qmth.examcloud.tool.utils;
  2. import cn.com.qmth.examcloud.tool.config.Constants;
  3. import okhttp3.*;
  4. import org.apache.commons.collections4.MapUtils;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import java.io.IOException;
  9. import java.io.UnsupportedEncodingException;
  10. import java.net.URLEncoder;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Map;
  14. public class HttpHelper {
  15. private static final Logger log = LoggerFactory.getLogger(HttpHelper.class);
  16. public static String get(String url, Map<String, String> headers, Map<String, String> params) {
  17. String urlParams = toUrlParams(params);
  18. log.debug("[GET] {}, headers: {}, params: {}", url, headers, urlParams);
  19. String and = url.indexOf("?") < 1 ? "?" : "&";
  20. Request.Builder builder = new Request.Builder().get().url(url + and + urlParams);
  21. return call(url, headers, builder);
  22. }
  23. public static String post(String url, Map<String, String> headers, Map<String, Object> params) {
  24. String jsonParams = "{}";
  25. if (MapUtils.isNotEmpty(params)) {
  26. jsonParams = new JsonMapper().toJson(params);
  27. }
  28. log.debug("[POST] {}, headers: {}, params: {}", url, headers, jsonParams);
  29. RequestBody requestBody = FormBody.create(jsonParams, MediaType.parse(Constants.CONTENT_TYPE_JSON));
  30. Request.Builder builder = new Request.Builder().post(requestBody).url(url);
  31. return call(url, headers, builder);
  32. }
  33. public static String put(String url, Map<String, String> headers, Map<String, Object> params) {
  34. String jsonParams = "{}";
  35. if (MapUtils.isNotEmpty(params)) {
  36. jsonParams = new JsonMapper().toJson(params);
  37. }
  38. log.debug("[PUT] {}, headers: {}, params: {}", url, headers, jsonParams);
  39. RequestBody requestBody = FormBody.create(jsonParams, MediaType.parse(Constants.CONTENT_TYPE_JSON));
  40. Request.Builder builder = new Request.Builder().put(requestBody).url(url);
  41. return call(url, headers, builder);
  42. }
  43. private static String call(String url, Map<String, String> headers, Request.Builder builder) {
  44. if (MapUtils.isNotEmpty(headers)) {
  45. for (Map.Entry<String, String> entry : headers.entrySet()) {
  46. builder.addHeader(entry.getKey(), entry.getValue());
  47. }
  48. }
  49. Request request = builder.build();
  50. try (Response response = HttpClientBuilder.getClient().newCall(request).execute();
  51. ResponseBody body = response.body();) {
  52. String bodyStr = body.string();
  53. if (response.isSuccessful()) {
  54. return bodyStr;
  55. }
  56. log.error("{}, response code: {}, body: {}", url, response.code(), bodyStr);
  57. throw new StatusException(bodyStr);
  58. } catch (IOException e) {
  59. log.error("{}, error: {}", url, e.getMessage());
  60. throw new StatusException("接口调用失败!");
  61. }
  62. }
  63. public static String toUrlParams(Map<String, String> params) {
  64. if (MapUtils.isEmpty(params)) {
  65. return "";
  66. }
  67. List<String> pair = new ArrayList<>();
  68. for (Map.Entry<String, String> entry : params.entrySet()) {
  69. pair.add(entry.getKey() + "=" + urlEncode(entry.getValue()));
  70. }
  71. return StringUtils.join(pair, "&");
  72. }
  73. public static String urlEncode(String value) {
  74. if (StringUtils.isEmpty(value)) {
  75. return "";
  76. }
  77. try {
  78. return URLEncoder.encode(value, "UTF-8");
  79. } catch (UnsupportedEncodingException e) {
  80. throw new RuntimeException(e);
  81. }
  82. }
  83. }