PageRenderTime 451ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/danlu/matrix/controller/manage/HttpRequest.java

https://gitlab.com/GavinGJ/MATIRX
Java | 137 lines | 94 code | 7 blank | 36 comment | 11 complexity | 838c89f4b9d9991a400ac10265c9c2ce MD5 | raw file
  1. package com.danlu.matrix.controller.manage;
  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. import com.alibaba.fastjson.JSONObject;
  11. public class HttpRequest {
  12. /**
  13. * 向指定URL发送GET方法的请求
  14. *
  15. * @param url
  16. * 发送请求的URL
  17. * @param param
  18. * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  19. * @return URL 所代表远程资源的响应结果
  20. */
  21. public static String sendGet(String url, String param) {
  22. String result = "";
  23. BufferedReader in = null;
  24. try {
  25. String urlNameString = url + "?" + param;
  26. URL realUrl = new URL(urlNameString);
  27. // 打开和URL之间的连接
  28. URLConnection connection = realUrl.openConnection();
  29. // 设置通用的请求属性
  30. connection.setRequestProperty("accept", "*/*");
  31. connection.setRequestProperty("connection", "Keep-Alive");
  32. connection.setRequestProperty("user-agent",
  33. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  34. // 建立实际的连接
  35. connection.connect();
  36. // 获取所有响应头字段
  37. Map<String, List<String>> map = connection.getHeaderFields();
  38. // 遍历所有的响应头字段
  39. for (String key : map.keySet()) {
  40. System.out.println(key + "--->" + map.get(key));
  41. }
  42. // 定义 BufferedReader输入流来读取URL的响应
  43. in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  44. String line;
  45. while ((line = in.readLine()) != null) {
  46. result += line;
  47. }
  48. } catch (Exception e) {
  49. System.out.println("发送GET请求出现异常!" + e);
  50. e.printStackTrace();
  51. }
  52. // 使用finally块来关闭输入流
  53. finally {
  54. try {
  55. if (in != null) {
  56. in.close();
  57. }
  58. } catch (Exception e2) {
  59. e2.printStackTrace();
  60. }
  61. }
  62. return result;
  63. }
  64. /**
  65. * 向指定 URL 发送POST方法的请求
  66. *
  67. * @param url
  68. * 发送请求的 URL
  69. * @param param
  70. * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  71. * @return 所代表远程资源的响应结果
  72. */
  73. public static String sendPost(String url, String param) {
  74. PrintWriter out = null;
  75. BufferedReader in = null;
  76. String result = "";
  77. try {
  78. URL realUrl = new URL(url);
  79. // 打开和URL之间的连接
  80. URLConnection conn = realUrl.openConnection();
  81. // 设置通用的请求属性
  82. conn.setRequestProperty("accept", "*/*");
  83. conn.setRequestProperty("connection", "Keep-Alive");
  84. conn.setRequestProperty("user-agent",
  85. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  86. // 发送POST请求必须设置如下两行
  87. conn.setDoOutput(true);
  88. conn.setDoInput(true);
  89. // 获取URLConnection对象对应的输出流
  90. out = new PrintWriter(conn.getOutputStream());
  91. // 发送请求参数
  92. out.print(param);
  93. // flush输出流的缓冲
  94. out.flush();
  95. // 定义BufferedReader输入流来读取URL的响应
  96. in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  97. String line;
  98. while ((line = in.readLine()) != null) {
  99. result += line;
  100. }
  101. } catch (Exception e) {
  102. System.out.println("发送 POST 请求出现异常!" + e);
  103. e.printStackTrace();
  104. }
  105. //使用finally块来关闭输出流、输入流
  106. finally {
  107. try {
  108. if (out != null) {
  109. out.close();
  110. }
  111. if (in != null) {
  112. in.close();
  113. }
  114. } catch (IOException ex) {
  115. ex.printStackTrace();
  116. }
  117. }
  118. return result;
  119. }
  120. public static void main(String[] args) {
  121. //发送 GET 请求
  122. String html = HttpRequest.sendGet("http://123.57.228.235:8082/dleye-web/switch_list.html", "ip=pay.danlu.com&name=&port=8090");
  123. //发送 GET 请求
  124. String Switch = HttpRequest.sendGet("http://pay.danlu.com:8090//dleye.do", "type=SWITCH&action=list");
  125. System.out.println("switch____"+Switch);
  126. //发送 GET 请求
  127. String Quariz = HttpRequest.sendGet("http://pay.danlu.com:8090//dleye.do", "type=QUARTZ&action=list");
  128. System.out.println("quariz____"+Quariz);
  129. }
  130. }