HttpUtil.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.yihu.wlyy.util;
  2. import org.json.JSONObject;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Component;
  7. import java.io.*;
  8. import java.net.HttpURLConnection;
  9. import java.net.URL;
  10. import java.net.URLConnection;
  11. /*
  12. * MD5 算法
  13. */
  14. @Component
  15. public class HttpUtil {
  16. private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
  17. @Autowired
  18. private ImUtill ImUtill;
  19. /**
  20. * 向指定URL发送GET方法的请求
  21. *
  22. * @param url
  23. * 发送请求的URL
  24. * @param param
  25. * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  26. * @return URL 所代表远程资源的响应结果
  27. */
  28. public String sendGet(String url, String param) {
  29. String result = "";
  30. BufferedReader in = null;
  31. try {
  32. String urlNameString = url + "?" + param;
  33. URL realUrl = new URL(urlNameString);
  34. // 打开和URL之间的连接
  35. URLConnection connection = realUrl.openConnection();
  36. // 设置通用的请求属性
  37. connection.setRequestProperty("accept", "*/*");
  38. connection.setRequestProperty("connection", "Keep-Alive");
  39. connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  40. // 建立实际的连接
  41. connection.connect();
  42. // 定义 BufferedReader输入流来读取URL的响应
  43. in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  44. String line;
  45. while ((line = in.readLine()) != null) {
  46. result += line;
  47. }
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. } finally {
  51. try {
  52. if (in != null) {
  53. in.close();
  54. }
  55. } catch (Exception e2) {
  56. e2.printStackTrace();
  57. }
  58. }
  59. return result;
  60. }
  61. /**
  62. * 向指定 URL 发送POST方法的请求
  63. *
  64. * @param url
  65. * 发送请求的 URL带上参数
  66. * @param param
  67. * POST参数。
  68. * @return 所代表远程资源的响应结果
  69. */
  70. public String sendPost(String url, String param) {
  71. StringBuffer buffer = new StringBuffer();
  72. PrintWriter out = null;
  73. BufferedReader in = null;
  74. HttpURLConnection conn = null;
  75. try {
  76. URL realUrl = new URL(url);
  77. // 打开和URL之间的连接
  78. conn = (HttpURLConnection) realUrl.openConnection();
  79. conn.setRequestMethod("POST");
  80. conn.setConnectTimeout(5000);
  81. conn.setDoOutput(true);
  82. conn.setDoInput(true);
  83. conn.setUseCaches(false);
  84. conn.setRequestProperty("Content-Type", "application/text");
  85. conn.setRequestProperty("accept", "*/*");
  86. conn.setRequestProperty("connection", "Keep-Alive");
  87. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  88. OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
  89. osw.write(param.toString());
  90. osw.flush();
  91. // 读取返回内容
  92. BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
  93. String temp;
  94. while ((temp = br.readLine()) != null) {
  95. buffer.append(temp);
  96. buffer.append("\n");
  97. }
  98. } catch (Exception e) {
  99. logger.error("push message error:", e);
  100. } finally {
  101. try {
  102. if (out != null) {
  103. out.close();
  104. }
  105. if (in != null) {
  106. in.close();
  107. }
  108. } catch (IOException ex) {
  109. ex.printStackTrace();
  110. }
  111. }
  112. return buffer.toString();
  113. }
  114. /**
  115. * 消息推送
  116. *
  117. * @param receiver 消息接收人
  118. * @param businessType 消息类型
  119. * @param title 消息标题
  120. * @param msg 消息内容
  121. * @param data 消息数据
  122. */
  123. public boolean pushMessage(String receiver, String businessType, String title, String msg, String data) {
  124. try{
  125. JSONObject participants = new JSONObject();
  126. participants.put("system",0);
  127. participants.put(receiver,0);
  128. JSONObject sessionObj = ImUtill.createSession(participants,ImUtill.SESSION_TYPE_SYSTEM,"系统消息","");
  129. if(sessionObj.getInt("status")==-1){
  130. throw new RuntimeException(sessionObj.getString("message"));
  131. }
  132. JSONObject session = sessionObj.getJSONObject("data");
  133. ImUtill.sendImMsg("system","系统",session.getString("id"),"1", msg,businessType);
  134. return true;
  135. }catch (Exception e){
  136. e.printStackTrace();
  137. }
  138. return false;
  139. }
  140. /**
  141. * 向指定 URL 发送POST方法的请求
  142. *
  143. * @param url 发送请求的 URL带上参数
  144. * @param param POST参数。
  145. * @param charset 编码格式
  146. * @return 所代表远程资源的响应结果
  147. */
  148. public String sendPost(String url, String param, String charset) {
  149. StringBuffer buffer = new StringBuffer();
  150. PrintWriter out = null;
  151. BufferedReader in = null;
  152. HttpURLConnection conn = null;
  153. try {
  154. URL realUrl = new URL(url);
  155. // 打开和URL之间的连接
  156. conn = (HttpURLConnection) realUrl.openConnection();
  157. conn.setRequestMethod("POST");
  158. conn.setDoOutput(true);
  159. conn.setDoInput(true);
  160. conn.setUseCaches(false);
  161. conn.setRequestProperty("Content-Type", "application/text");
  162. conn.setRequestProperty("accept", "*/*");
  163. conn.setRequestProperty("connection", "Keep-Alive");
  164. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  165. OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), charset);
  166. osw.write(param.toString());
  167. osw.flush();
  168. // 读取返回内容
  169. BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
  170. String temp;
  171. while ((temp = br.readLine()) != null) {
  172. buffer.append(temp);
  173. buffer.append("\n");
  174. }
  175. } catch (Exception e) {
  176. logger.error("push message error:", e);
  177. } finally {
  178. try {
  179. if (out != null) {
  180. out.close();
  181. }
  182. if (in != null) {
  183. in.close();
  184. }
  185. } catch (IOException ex) {
  186. ex.printStackTrace();
  187. }
  188. }
  189. return buffer.toString();
  190. }
  191. }