HttpUtil.java 8.6 KB

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