RequestUtils.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package com.template.common.utils;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.util.List;
  9. import java.util.Map;
  10. /**
  11. * @Author: binguo
  12. * @Date: 2023/12/21 星期四 9:39
  13. * @Description: com.template.common.utils
  14. * @Version: 1.0
  15. */
  16. public class RequestUtils {
  17. /**
  18. * GET请求
  19. * @param url 请求地址
  20. * @param param 请求参数
  21. * @return
  22. */
  23. public static String httpGet(String url,String param){
  24. String result = "";
  25. BufferedReader in = null;
  26. try {
  27. String urlNameString = url + "?" + param;
  28. URL realUrl = new URL(urlNameString);
  29. // 打开和URL之间的连接
  30. URLConnection connection = realUrl.openConnection();
  31. // 设置通用的请求属性
  32. connection.setRequestProperty("accept", "*/*");
  33. connection.setRequestProperty("connection", "Keep-Alive");
  34. connection.setRequestProperty("user-agent",
  35. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  36. // 建立实际的连接
  37. connection.connect();
  38. // 获取所有响应头字段
  39. Map<String, List<String>> map = connection.getHeaderFields();
  40. // 遍历所有的响应头字段
  41. for (String key : map.keySet()) {
  42. System.out.println(key + "--->" + map.get(key));
  43. }
  44. // 定义 BufferedReader输入流来读取URL的响应
  45. in = new BufferedReader(new InputStreamReader(
  46. connection.getInputStream()));
  47. String line;
  48. while ((line = in.readLine()) != null) {
  49. result += line;
  50. }
  51. } catch (Exception e) {
  52. System.out.println("发送GET请求出现异常!" + e);
  53. e.printStackTrace();
  54. }
  55. // 使用finally块来关闭输入流
  56. finally {
  57. try {
  58. if (in != null) {
  59. in.close();
  60. }
  61. } catch (Exception e2) {
  62. e2.printStackTrace();
  63. }
  64. }
  65. return result;
  66. }
  67. /**
  68. * POST请求
  69. * @param url 请求的url
  70. * @param param 参数
  71. * @return
  72. */
  73. public static String httpPost(String url, String param){
  74. String dummyData = param.replace("%", "").replace(",", "").replace(" ", "+").replace("/","");
  75. PrintWriter out = null;
  76. BufferedReader in = null;
  77. String result = "";
  78. try {
  79. URL realUrl = new URL(url);
  80. // 打开和URL之间的连接
  81. URLConnection conn = realUrl.openConnection();
  82. // 设置通用的请求属性
  83. conn.setRequestProperty("accept", "*/*");
  84. conn.setRequestProperty("connection", "Keep-Alive");
  85. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  86. conn.setRequestProperty("Content-Type", "application/json");
  87. // 发送POST请求必须设置如下两行
  88. conn.setDoOutput(true);
  89. conn.setDoInput(true);
  90. // 获取URLConnection对象对应的输出流
  91. out = new PrintWriter(conn.getOutputStream());
  92. // 发送请求参数
  93. out.print(param);
  94. // flush输出流的缓冲
  95. out.flush();
  96. // 定义BufferedReader输入流来读取URL的响应
  97. in = new BufferedReader(
  98. new InputStreamReader(conn.getInputStream()));
  99. String line;
  100. while ((line = in.readLine()) != null) {
  101. result += line;
  102. }
  103. } catch (Exception e) {
  104. System.out.println("发送 POST 请求出现异常!" + e);
  105. e.printStackTrace();
  106. }
  107. //使用finally块来关闭输出流、输入流
  108. finally {
  109. try {
  110. if (out != null) {
  111. out.close();
  112. }
  113. if (in != null) {
  114. in.close();
  115. }
  116. } catch (IOException ex) {
  117. ex.printStackTrace();
  118. }
  119. }
  120. return result;
  121. }
  122. }