JAVA通过HTTP访问:Post+Get方式(转)

时间:2023-03-09 04:38:30
JAVA通过HTTP访问:Post+Get方式(转)
  1. public class TestGetPost {
  2. /**
  3. * 向指定URL发送GET方法的请求
  4. * @param url 发送请求的URL
  5. * @param param 请求参数,请求参数应该是name1=value1&name2=value2的形式。
  6. * @return URL所代表远程资源的响应
  7. */
  8. public static String sendGet(String url, String param) {
  9. String result = "";
  10. BufferedReader in = null;
  11. try {
  12. String urlName = url + "?" + param;
  13. URL realUrl = new URL(urlName);
  14. //打开和URL之间的连接
  15. URLConnection conn = realUrl.openConnection();
  16. //设置通用的请求属性
  17. conn.setRequestProperty("accept", "*/*");
  18. conn.setRequestProperty("connection", "Keep-Alive");
  19. conn.setRequestProperty("user-agent",
  20. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
  21. //建立实际的连接
  22. conn.connect();
  23. //获取所有响应头字段
  24. Map < String, List < String >> map = conn.getHeaderFields();
  25. //遍历所有的响应头字段
  26. for (String key: map.keySet()) {
  27. System.out.println(key + "--->" + map.get(key));
  28. }
  29. //定义BufferedReader输入流来读取URL的响应
  30. in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  31. String line;
  32. while ((line = in .readLine()) != null) {
  33. result += "/n" + line;
  34. }
  35. } catch (Exception e) {
  36. System.out.println("发送GET请求出现异常!" + e);
  37. e.printStackTrace();
  38. }
  39. //使用finally块来关闭输入流
  40. finally {
  41. try {
  42. if ( in != null) { in .close();
  43. }
  44. } catch (IOException ex) {
  45. ex.printStackTrace();
  46. }
  47. }
  48. return result;
  49. }
  50. /**
  51. * 向指定URL发送POST方法的请求
  52. * @param url 发送请求的URL
  53. * @param param 请求参数,请求参数应该是name1=value1&name2=value2的形式。
  54. * @return URL所代表远程资源的响应
  55. */
  56. public static String sendPost(String url, String param) {
  57. PrintWriter out = null;
  58. BufferedReader in = null;
  59. String result = "";
  60. try {
  61. URL realUrl = new URL(url);
  62. //打开和URL之间的连接
  63. URLConnection conn = realUrl.openConnection();
  64. //设置通用的请求属性
  65. conn.setRequestProperty("accept", "*/*");
  66. conn.setRequestProperty("connection", "Keep-Alive");
  67. conn.setRequestProperty("user-agent",
  68. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
  69. //发送POST请求必须设置如下两行
  70. conn.setDoOutput(true);
  71. conn.setDoInput(true);
  72. //获取URLConnection对象对应的输出流
  73. out = new PrintWriter(conn.getOutputStream());
  74. //发送请求参数
  75. out.print(param);
  76. //flush输出流的缓冲
  77. out.flush();
  78. //定义BufferedReader输入流来读取URL的响应
  79. in = new BufferedReader(
  80. new InputStreamReader(conn.getInputStream()));
  81. String line;
  82. while ((line = in .readLine()) != null) {
  83. result += "/n" + line;
  84. }
  85. } catch (Exception e) {
  86. System.out.println("发送POST请求出现异常!" + e);
  87. e.printStackTrace();
  88. }
  89. //使用finally块来关闭输出流、输入流
  90. finally {
  91. try {
  92. if (out != null) {
  93. out.close();
  94. }
  95. if ( in != null) { in .close();
  96. }
  97. } catch (IOException ex) {
  98. ex.printStackTrace();
  99. }
  100. }
  101. return result;
  102. }
  103. //提供主方法,测试发送GET请求和POST请求
  104. public static void main(String args[]) {
  105. //发送GET请求
  106. String s = TestGetPost.sendGet("http://localhost:8888/abc/login.jsp", null);
  107. System.out.println(s);
  108. //发送POST请求
  109. String s1 = TestGetPost.sendPost(http: //localhost:8888/abc/a.jsp ,
  110. "user=李刚&pass=abc");
  111. System.out.println(s1);
  112. }
  113. }

相关文章