1、http请求发送-工具类

  1. /**
  2. * http请求发送
  3. */
  4. @Slf4j
  5. public class HttpClientUtil {
  6. public static String sendGet(String uri, String headerName, String headerValue)
  7. throws IOException {
  8. Content content = Request.Get(uri).setHeader(headerName, headerValue)
  9. .execute().returnContent();
  10. return content.asString();
  11. }
  12. public static String sendPost(String url, String body, String contentType, String headerName, String headerValue)
  13. throws IOException {
  14. Content content = Request.Post(url)
  15. .setHeader(headerName, headerValue)
  16. .bodyString(body, ContentType.create(contentType))
  17. .execute().returnContent();
  18. return content.asString();
  19. }
  20. public static String toGMTString(Date date) {
  21. SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
  22. df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
  23. return df.format(date);
  24. }
  25. public static String sendPost(String url, String body, String akId, String akSecret) {
  26. return sendPostOrPut(url, "POST", body, akId, akSecret);
  27. }
  28. public static String sendPut(String url, String body, String akId, String akSecret) {
  29. return sendPostOrPut(url, "PUT", body, akId, akSecret);
  30. }
  31. private static String sendPostOrPut(String url, String method, String body, String akId, String akSecret) {
  32. PrintWriter out = null;
  33. BufferedReader in = null;
  34. String result = "";
  35. try {
  36. URL realUrl = new URL(url);
  37. String accept = "json";
  38. String contentType = "application/json";
  39. String path = realUrl.getFile();
  40. String date = toGMTString(new Date());
  41. String bodyMd5 = DigestUtil.md5Base64(body);
  42. String stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + contentType + "\n" + date + "\n"
  43. + path;
  44. String signature = DigestUtil.hmacsha1(stringToSign, akSecret);
  45. String authHeader = "dtboost-proxy " + akId + ":" + signature;
  46. HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
  47. conn.setRequestMethod(method);
  48. conn.setRequestProperty("accept", accept);
  49. conn.setRequestProperty("content-type", contentType);
  50. conn.setRequestProperty("date", date);
  51. conn.setRequestProperty("Authorization", authHeader);
  52. conn.setRequestProperty("Content-MD5", bodyMd5);
  53. conn.setDoOutput(true);
  54. conn.setDoInput(true);
  55. out = new PrintWriter(conn.getOutputStream());
  56. out.print(body);
  57. out.flush();
  58. if (conn.getResponseCode() != 200) {
  59. in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
  60. } else {
  61. in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  62. }
  63. String line;
  64. while ((line = in.readLine()) != null) {
  65. result += line;
  66. }
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. } finally {
  70. try {
  71. if (out != null) {
  72. out.close();
  73. }
  74. if (in != null) {
  75. in.close();
  76. }
  77. } catch (IOException ex) {
  78. ex.printStackTrace();
  79. }
  80. }
  81. return result;
  82. }
  83. public static String sendAKGet(String url, String akId, String akSecret) {
  84. String result = "";
  85. BufferedReader in = null;
  86. try {
  87. URL realUrl = new URL(url);
  88. String method = "GET";
  89. String accept = "json";
  90. String contentType = "application/json";
  91. String path = realUrl.getFile();
  92. String date = toGMTString(new Date());
  93. String stringToSign = method + "\n" + accept + "\n" + "" + "\n" + contentType + "\n" + date + "\n" + path;
  94. String signature = DigestUtil.hmacsha1(stringToSign, akSecret);
  95. String authHeader = "dtboost-proxy " + akId + ":" + signature;
  96. HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
  97. connection.setRequestProperty("accept", accept);
  98. connection.setRequestProperty("content-type", contentType);
  99. connection.setRequestProperty("date", date);
  100. connection.setRequestProperty("Authorization", authHeader);
  101. connection.connect();
  102. if (connection.getResponseCode() != 200) {
  103. in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
  104. } else {
  105. in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  106. }
  107. String line;
  108. while ((line = in.readLine()) != null) {
  109. result += line;
  110. }
  111. } catch (Exception e) {
  112. e.printStackTrace();
  113. } finally {
  114. try {
  115. if (in != null) {
  116. in.close();
  117. }
  118. } catch (Exception e) {
  119. e.printStackTrace();
  120. }
  121. }
  122. return result;
  123. }
  124. /**
  125. * http head请求
  126. * @throws Exception
  127. */
  128. public static String sendHeadGet(String path, Long projectId) throws Exception{
  129. String value = "{'extraInfo':{'projectId':'"+projectId+"'}}";
  130. try {
  131. URL url = new URL(path);
  132. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  133. //connection.setRequestProperty("设置请求头key", "请求头value");
  134. connection.addRequestProperty("CityBrain-User-Profile", value);
  135. //连接会话
  136. connection.connect();
  137. // 获取输入流
  138. BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
  139. String line;
  140. StringBuilder sb = new StringBuilder();
  141. while (Objects.nonNull((line = br.readLine()))) {
  142. sb.append(line);
  143. }
  144. br.close();
  145. connection.disconnect();
  146. String result = sb.toString();
  147. return result;
  148. } catch (Exception e) {
  149. log.error("请求失败,url:{}", path);
  150. throw e;
  151. }
  152. }
  153. }