HttpUtil.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package com.yihu.figure.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,String userAgent) {
  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. connection.setRequestProperty("user-agent", userAgent);
  35. // 建立实际的连接
  36. connection.connect();
  37. // 定义 BufferedReader输入流来读取URL的响应
  38. in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  39. String line;
  40. while ((line = in.readLine()) != null) {
  41. result += line;
  42. }
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. } finally {
  46. try {
  47. if (in != null) {
  48. in.close();
  49. }
  50. } catch (Exception e2) {
  51. e2.printStackTrace();
  52. }
  53. }
  54. return result;
  55. }
  56. /**
  57. * 向指定 URL 发送POST方法的请求
  58. *
  59. * @param url
  60. * 发送请求的 URL带上参数
  61. * @param param
  62. * POST参数。
  63. * @return 所代表远程资源的响应结果
  64. */
  65. public static String sendPost(String url, String param) {
  66. StringBuffer buffer = new StringBuffer();
  67. PrintWriter out = null;
  68. BufferedReader in = null;
  69. HttpURLConnection conn = null;
  70. try {
  71. URL realUrl = new URL(url);
  72. // 打开和URL之间的连接
  73. conn = (HttpURLConnection) realUrl.openConnection();
  74. conn.setRequestMethod("POST");
  75. conn.setConnectTimeout(5000);
  76. conn.setDoOutput(true);
  77. conn.setDoInput(true);
  78. conn.setUseCaches(false);
  79. conn.setRequestProperty("Content-Type", "application/text");
  80. conn.setRequestProperty("accept", "*/*");
  81. conn.setRequestProperty("connection", "Keep-Alive");
  82. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  83. OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
  84. osw.write(param.toString());
  85. osw.flush();
  86. // 读取返回内容
  87. BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
  88. String temp;
  89. while ((temp = br.readLine()) != null) {
  90. buffer.append(temp);
  91. buffer.append("\n");
  92. }
  93. } catch (Exception e) {
  94. logger.error("push message error:", e);
  95. } finally {
  96. try {
  97. if (out != null) {
  98. out.close();
  99. }
  100. if (in != null) {
  101. in.close();
  102. }
  103. } catch (IOException ex) {
  104. ex.printStackTrace();
  105. }
  106. }
  107. return buffer.toString();
  108. }
  109. /**
  110. * 向指定 URL 发送POST方法的请求
  111. *
  112. * @param url 发送请求的 URL带上参数
  113. * @param param POST参数。
  114. * @param charset 编码格式
  115. * @return 所代表远程资源的响应结果
  116. */
  117. public static String sendPost(String url, String param, String charset) {
  118. StringBuffer buffer = new StringBuffer();
  119. PrintWriter out = null;
  120. BufferedReader in = null;
  121. HttpURLConnection conn = null;
  122. try {
  123. URL realUrl = new URL(url);
  124. // 打开和URL之间的连接
  125. conn = (HttpURLConnection) realUrl.openConnection();
  126. conn.setRequestMethod("POST");
  127. conn.setDoOutput(true);
  128. conn.setDoInput(true);
  129. conn.setUseCaches(false);
  130. conn.setRequestProperty("Content-Type", "application/text");
  131. conn.setRequestProperty("accept", "*/*");
  132. conn.setRequestProperty("connection", "Keep-Alive");
  133. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  134. OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), charset);
  135. osw.write(param.toString());
  136. osw.flush();
  137. // 读取返回内容
  138. BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
  139. String temp;
  140. while ((temp = br.readLine()) != null) {
  141. buffer.append(temp);
  142. buffer.append("\n");
  143. }
  144. } catch (Exception e) {
  145. logger.error("push message error:", e);
  146. } finally {
  147. try {
  148. if (out != null) {
  149. out.close();
  150. }
  151. if (in != null) {
  152. in.close();
  153. }
  154. } catch (IOException ex) {
  155. ex.printStackTrace();
  156. }
  157. }
  158. return buffer.toString();
  159. }
  160. public static void main(String[] args) {
  161. }
  162. }