HttpRequest.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.common.workflow.service.util;
  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 = "";
  25. if(url.indexOf("?") > -1) {
  26. urlNameString = url + "&" + param;
  27. } else {
  28. urlNameString = url + "?" + param;
  29. }
  30. URL realUrl = new URL(urlNameString);
  31. // 打开和URL之间的连接
  32. URLConnection connection = realUrl.openConnection();
  33. // 设置通用的请求属性
  34. connection.setRequestProperty("accept", "*/*");
  35. connection.setRequestProperty("connection", "Keep-Alive");
  36. connection.setRequestProperty("user-agent",
  37. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  38. // 建立实际的连接
  39. connection.connect();
  40. // 获取所有响应头字段
  41. Map<String, List<String>> map = connection.getHeaderFields();
  42. // 遍历所有的响应头字段
  43. for (String key : map.keySet()) {
  44. System.out.println(key + "--->" + map.get(key));
  45. }
  46. // 定义 BufferedReader输入流来读取URL的响应
  47. in = new BufferedReader(new InputStreamReader(
  48. connection.getInputStream()));
  49. String line;
  50. while ((line = in.readLine()) != null) {
  51. result += line;
  52. }
  53. } catch (Exception e) {
  54. System.out.println("发送GET请求出现异常!" + e);
  55. e.printStackTrace();
  56. }
  57. // 使用finally块来关闭输入流
  58. finally {
  59. try {
  60. if (in != null) {
  61. in.close();
  62. }
  63. } catch (Exception e2) {
  64. e2.printStackTrace();
  65. }
  66. }
  67. return result;
  68. }
  69. /**
  70. * 向指定 URL 发送POST方法的请求
  71. *
  72. * @param url
  73. * 发送请求的 URL
  74. * @param param
  75. * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  76. * @return 所代表远程资源的响应结果
  77. */
  78. public static String sendPost(String url, String param) {
  79. PrintWriter out = null;
  80. BufferedReader in = null;
  81. String result = "";
  82. try {
  83. URL realUrl = new URL(url);
  84. // 打开和URL之间的连接
  85. URLConnection conn = realUrl.openConnection();
  86. // 设置通用的请求属性
  87. conn.setRequestProperty("accept", "*/*");
  88. conn.setRequestProperty("connection", "Keep-Alive");
  89. conn.setRequestProperty("user-agent",
  90. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  91. // 发送POST请求必须设置如下两行
  92. conn.setDoOutput(true);
  93. conn.setDoInput(true);
  94. // 获取URLConnection对象对应的输出流
  95. out = new PrintWriter(conn.getOutputStream());
  96. // 发送请求参数
  97. out.print(param);
  98. // flush输出流的缓冲
  99. out.flush();
  100. // 定义BufferedReader输入流来读取URL的响应
  101. in = new BufferedReader(
  102. new InputStreamReader(conn.getInputStream()));
  103. String line;
  104. while ((line = in.readLine()) != null) {
  105. result += line;
  106. }
  107. } catch (Exception e) {
  108. System.out.println("发送 POST 请求出现异常!"+e);
  109. e.printStackTrace();
  110. }
  111. //使用finally块来关闭输出流、输入流
  112. finally{
  113. try{
  114. if(out!=null){
  115. out.close();
  116. }
  117. if(in!=null){
  118. in.close();
  119. }
  120. }
  121. catch(IOException ex){
  122. ex.printStackTrace();
  123. }
  124. }
  125. return result;
  126. }
  127. }