HttpRequest.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package qh.lqg.utils.net;
  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. public class HttpRequest {
  11. /**
  12. * 向指定URL发送GET方法的请求
  13. *
  14. * @param url
  15. * 发送请求的URL
  16. * @param param
  17. * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  18. * @return URL 所代表远程资源的响应结果
  19. */
  20. public static String sendGet(String url, String param) {
  21. String result = "";
  22. BufferedReader in = null;
  23. try {
  24. String urlNameString = url + "?" + param;
  25. URL realUrl = new URL(urlNameString);
  26. // 打开和URL之间的连接
  27. URLConnection connection = realUrl.openConnection();
  28. // 设置通用的请求属性
  29. connection.setRequestProperty("accept", "*/*");
  30. connection.setRequestProperty("connection", "Keep-Alive");
  31. connection.setRequestProperty("user-agent",
  32. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  33. // 建立实际的连接
  34. connection.connect();
  35. // 获取所有响应头字段
  36. Map<String, List<String>> map = connection.getHeaderFields();
  37. // 遍历所有的响应头字段
  38. for (String key : map.keySet()) {
  39. System.out.println(key + "--->" + map.get(key));
  40. }
  41. // 定义 BufferedReader输入流来读取URL的响应
  42. in = new BufferedReader(new InputStreamReader(
  43. 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(
  97. new InputStreamReader(conn.getInputStream()));
  98. String line;
  99. while ((line = in.readLine()) != null) {
  100. result += line;
  101. }
  102. } catch (Exception e) {
  103. System.out.println("发送 POST 请求出现异常!" + e);
  104. e.printStackTrace();
  105. }
  106. // 使用finally块来关闭输出流、输入流
  107. finally {
  108. try {
  109. if (out != null) {
  110. out.close();
  111. }
  112. if (in != null) {
  113. in.close();
  114. }
  115. } catch (IOException ex) {
  116. ex.printStackTrace();
  117. }
  118. }
  119. return result;
  120. }
  121. }