HttpUtil.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.yihu.jw.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import java.io.*;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. /*
  9. * MD5 算法
  10. */
  11. public class HttpUtil {
  12. private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
  13. /**
  14. * 向指定URL发送GET方法的请求
  15. *
  16. * @param url
  17. * 发送请求的URL
  18. * @param param
  19. * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  20. * @return URL 所代表远程资源的响应结果
  21. */
  22. public static String sendGet(String url, String param) {
  23. String result = "";
  24. BufferedReader in = null;
  25. try {
  26. String urlNameString = url + "?" + param;
  27. URL realUrl = new URL(urlNameString);
  28. // 打开和URL之间的连接
  29. URLConnection connection = realUrl.openConnection();
  30. // 设置通用的请求属性
  31. connection.setRequestProperty("accept", "*/*");
  32. connection.setRequestProperty("connection", "Keep-Alive");
  33. connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  34. // 建立实际的连接
  35. connection.connect();
  36. // 定义 BufferedReader输入流来读取URL的响应
  37. in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  38. String line;
  39. while ((line = in.readLine()) != null) {
  40. result += line;
  41. }
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. } finally {
  45. try {
  46. if (in != null) {
  47. in.close();
  48. }
  49. } catch (Exception e2) {
  50. e2.printStackTrace();
  51. }
  52. }
  53. return result;
  54. }
  55. /**
  56. * 向指定 URL 发送POST方法的请求
  57. *
  58. * @param url
  59. * 发送请求的 URL带上参数
  60. * @param param
  61. * POST参数。
  62. * @return 所代表远程资源的响应结果
  63. */
  64. public static String sendPost(String url, String param) {
  65. StringBuffer buffer = new StringBuffer();
  66. OutputStreamWriter osw = null;
  67. BufferedReader in = null;
  68. HttpURLConnection conn = null;
  69. try {
  70. URL realUrl = new URL(url);
  71. // 打开和URL之间的连接
  72. conn = (HttpURLConnection) realUrl.openConnection();
  73. conn.setRequestMethod("POST");
  74. conn.setConnectTimeout(5000);
  75. conn.setDoOutput(true);
  76. conn.setDoInput(true);
  77. conn.setUseCaches(false);
  78. conn.setRequestProperty("Content-Type", "application/text");
  79. conn.setRequestProperty("accept", "*/*");
  80. conn.setRequestProperty("connection", "Keep-Alive");
  81. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  82. osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
  83. osw.write(param.toString());
  84. osw.flush();
  85. // 读取返回内容
  86. in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
  87. String temp;
  88. while ((temp = in.readLine()) != null) {
  89. buffer.append(temp);
  90. buffer.append("\n");
  91. }
  92. } catch (Exception e) {
  93. logger.error("push message error:", e);
  94. } finally {
  95. try {
  96. if (osw != null) {
  97. osw.close();
  98. }
  99. if (in != null) {
  100. in.close();
  101. }
  102. } catch (IOException ex) {
  103. ex.printStackTrace();
  104. }
  105. }
  106. return buffer.toString();
  107. }
  108. }