HttpUtil.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package com.yihu.wlyy.util;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.io.PrintWriter;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import org.apache.http.NameValuePair;
  13. import org.apache.http.message.BasicNameValuePair;
  14. import org.json.JSONObject;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. /*
  18. * MD5 算法
  19. */
  20. public class HttpUtil {
  21. private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
  22. /**
  23. * 向指定URL发送GET方法的请求
  24. *
  25. * @param url
  26. * 发送请求的URL
  27. * @param param
  28. * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  29. * @return URL 所代表远程资源的响应结果
  30. */
  31. public static String sendGet(String url, String param) {
  32. String result = "";
  33. BufferedReader in = null;
  34. try {
  35. String urlNameString = url + "?" + param;
  36. URL realUrl = new URL(urlNameString);
  37. // 打开和URL之间的连接
  38. URLConnection connection = realUrl.openConnection();
  39. // 设置通用的请求属性
  40. connection.setRequestProperty("accept", "*/*");
  41. connection.setRequestProperty("connection", "Keep-Alive");
  42. connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  43. // 建立实际的连接
  44. connection.connect();
  45. // 定义 BufferedReader输入流来读取URL的响应
  46. in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  47. String line;
  48. while ((line = in.readLine()) != null) {
  49. result += line;
  50. }
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. } finally {
  54. try {
  55. if (in != null) {
  56. in.close();
  57. }
  58. } catch (Exception e2) {
  59. e2.printStackTrace();
  60. }
  61. }
  62. return result;
  63. }
  64. /**
  65. * 向指定 URL 发送POST方法的请求
  66. *
  67. * @param url
  68. * 发送请求的 URL带上参数
  69. * @param param
  70. * POST参数。
  71. * @return 所代表远程资源的响应结果
  72. */
  73. public static String sendPost(String url, String param) {
  74. StringBuffer buffer = new StringBuffer();
  75. PrintWriter out = null;
  76. BufferedReader in = null;
  77. HttpURLConnection conn = null;
  78. try {
  79. URL realUrl = new URL(url);
  80. // 打开和URL之间的连接
  81. conn = (HttpURLConnection) realUrl.openConnection();
  82. conn.setRequestMethod("POST");
  83. conn.setConnectTimeout(5000);
  84. conn.setDoOutput(true);
  85. conn.setDoInput(true);
  86. conn.setUseCaches(false);
  87. conn.setRequestProperty("Content-Type", "application/text");
  88. conn.setRequestProperty("accept", "*/*");
  89. conn.setRequestProperty("connection", "Keep-Alive");
  90. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  91. OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
  92. osw.write(param.toString());
  93. osw.flush();
  94. // 读取返回内容
  95. BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
  96. String temp;
  97. while ((temp = br.readLine()) != null) {
  98. buffer.append(temp);
  99. buffer.append("\n");
  100. }
  101. } catch (Exception e) {
  102. logger.error("push message error:", e);
  103. } finally {
  104. try {
  105. if (out != null) {
  106. out.close();
  107. }
  108. if (in != null) {
  109. in.close();
  110. }
  111. } catch (IOException ex) {
  112. ex.printStackTrace();
  113. }
  114. }
  115. return buffer.toString();
  116. }
  117. /**
  118. * 消息推送
  119. *
  120. * @param receiver 消息接收人
  121. * @param businessType 消息类型
  122. * @param title 消息标题
  123. * @param msg 消息内容
  124. * @param data 消息数据
  125. */
  126. public static boolean pushMessage(String receiver, String businessType, String title, String msg, String data) {
  127. try{
  128. JSONObject participants = new JSONObject();
  129. participants.put("system",0);
  130. participants.put(receiver,0);
  131. JSONObject sessionObj = ImUtill.createSession(participants,"2","系统消息","");
  132. if(sessionObj.getInt("status")==-1){
  133. throw new RuntimeException(sessionObj.getString("message"));
  134. }
  135. JSONObject session = sessionObj.getJSONObject("data");
  136. ImUtill.sendImMsg("system","系统",session.getString("id"),"1", msg,businessType);
  137. return true;
  138. }catch (Exception e){
  139. e.printStackTrace();
  140. }
  141. return false;
  142. }
  143. /**
  144. * 发送消息到websocket服务器,然后由websocket服务器中转给微信端
  145. * @param userid 接收数据的患者id
  146. * @param data 内容
  147. * @return 推送成功:{"errno":"0","errmsg":""},推送失败:{"errno":"1","errmsg":"User is not online"}
  148. * @author shenzaixin
  149. */
  150. public static String sendWeixinWebsocketMsg(String userid,String data){
  151. PrintWriter out = null;
  152. BufferedReader in = null;
  153. HttpURLConnection conn = null;
  154. try {
  155. System.out.println("consult-send:" + userid + ":" + data);
  156. String url = SystemConf.getInstance().getWeixinWebsocketServer() + "?userid=" + userid + "&data=" + data;
  157. URL realUrl = new URL(url);
  158. // 打开和URL之间的连接
  159. conn = (HttpURLConnection) realUrl.openConnection();
  160. conn.setRequestMethod("GET");
  161. conn.setDoOutput(true);
  162. conn.setDoInput(true);
  163. conn.setUseCaches(false);
  164. conn.setRequestProperty("Content-Type", "application/json");
  165. // OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
  166. // osw.write(param.toString());
  167. // osw.flush();
  168. // 读取返回内容
  169. StringBuffer buffer = new StringBuffer();
  170. BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
  171. String temp;
  172. while ((temp = br.readLine()) != null) {
  173. buffer.append(temp);
  174. }
  175. System.out.println(buffer.toString());
  176. JSONObject json = new JSONObject(buffer.toString());
  177. System.out.println("consult-reply:" + json.toString());
  178. return json.toString();
  179. } catch (Exception e) {
  180. logger.error("push message error:", e);
  181. return "{\"errno\":\"1\",\"errmsg\":\""+e.getMessage()+"\"}";
  182. } finally {
  183. try {
  184. if (out != null) {
  185. out.close();
  186. }
  187. if (in != null) {
  188. in.close();
  189. }
  190. } catch (IOException ex) {
  191. ex.printStackTrace();
  192. }
  193. }
  194. }
  195. /**
  196. * 向指定 URL 发送POST方法的请求
  197. *
  198. * @param url 发送请求的 URL带上参数
  199. * @param param POST参数。
  200. * @param charset 编码格式
  201. * @return 所代表远程资源的响应结果
  202. */
  203. public static String sendPost(String url, String param, String charset) {
  204. StringBuffer buffer = new StringBuffer();
  205. PrintWriter out = null;
  206. BufferedReader in = null;
  207. HttpURLConnection conn = null;
  208. try {
  209. URL realUrl = new URL(url);
  210. // 打开和URL之间的连接
  211. conn = (HttpURLConnection) realUrl.openConnection();
  212. conn.setRequestMethod("POST");
  213. conn.setDoOutput(true);
  214. conn.setDoInput(true);
  215. conn.setUseCaches(false);
  216. conn.setRequestProperty("Content-Type", "application/text");
  217. conn.setRequestProperty("accept", "*/*");
  218. conn.setRequestProperty("connection", "Keep-Alive");
  219. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  220. OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), charset);
  221. osw.write(param.toString());
  222. osw.flush();
  223. // 读取返回内容
  224. BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
  225. String temp;
  226. while ((temp = br.readLine()) != null) {
  227. buffer.append(temp);
  228. buffer.append("\n");
  229. }
  230. } catch (Exception e) {
  231. logger.error("push message error:", e);
  232. } finally {
  233. try {
  234. if (out != null) {
  235. out.close();
  236. }
  237. if (in != null) {
  238. in.close();
  239. }
  240. } catch (IOException ex) {
  241. ex.printStackTrace();
  242. }
  243. }
  244. return buffer.toString();
  245. }
  246. public static void main(String[] args) {
  247. boolean result = HttpUtil.pushMessage("U20160322000001", "1", "您有一条医嘱提醒", "少吃辣,多运动,多吃水果!", null);
  248. System.out.println(result);
  249. }
  250. }