|
@ -22,7 +22,7 @@ public class HttpUtil {
|
|
|
|
|
|
/**
|
|
|
* 向指定URL发送GET方法的请求
|
|
|
*
|
|
|
*
|
|
|
* @param url
|
|
|
* 发送请求的URL
|
|
|
* @param param
|
|
@ -65,7 +65,7 @@ public class HttpUtil {
|
|
|
|
|
|
/**
|
|
|
* 向指定 URL 发送POST方法的请求
|
|
|
*
|
|
|
*
|
|
|
* @param url
|
|
|
* 发送请求的 URL带上参数
|
|
|
* @param param
|
|
@ -73,38 +73,38 @@ public class HttpUtil {
|
|
|
* @return 所代表远程资源的响应结果
|
|
|
*/
|
|
|
public static String sendPost(String url, String param) {
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
|
PrintWriter out = null;
|
|
|
BufferedReader in = null;
|
|
|
HttpURLConnection conn = null;
|
|
|
try {
|
|
|
URL realUrl = new URL(url);
|
|
|
// 打开和URL之间的连接
|
|
|
conn = (HttpURLConnection) realUrl.openConnection();
|
|
|
conn.setRequestMethod("POST");
|
|
|
conn.setDoOutput(true);
|
|
|
conn.setDoInput(true);
|
|
|
conn.setUseCaches(false);
|
|
|
conn.setRequestProperty("Content-Type", "application/text");
|
|
|
conn.setRequestProperty("accept", "*/*");
|
|
|
conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
|
|
|
osw.write(param.toString());
|
|
|
osw.flush();
|
|
|
|
|
|
// 读取返回内容
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
|
String temp;
|
|
|
while ((temp = br.readLine()) != null) {
|
|
|
buffer.append(temp);
|
|
|
buffer.append("\n");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error("push message error:", e);
|
|
|
} finally {
|
|
|
try {
|
|
|
if (out != null) {
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
|
PrintWriter out = null;
|
|
|
BufferedReader in = null;
|
|
|
HttpURLConnection conn = null;
|
|
|
try {
|
|
|
URL realUrl = new URL(url);
|
|
|
// 打开和URL之间的连接
|
|
|
conn = (HttpURLConnection) realUrl.openConnection();
|
|
|
conn.setRequestMethod("POST");
|
|
|
conn.setDoOutput(true);
|
|
|
conn.setDoInput(true);
|
|
|
conn.setUseCaches(false);
|
|
|
conn.setRequestProperty("Content-Type", "application/text");
|
|
|
conn.setRequestProperty("accept", "*/*");
|
|
|
conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
|
|
|
osw.write(param.toString());
|
|
|
osw.flush();
|
|
|
|
|
|
// 读取返回内容
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
|
String temp;
|
|
|
while ((temp = br.readLine()) != null) {
|
|
|
buffer.append(temp);
|
|
|
buffer.append("\n");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error("push message error:", e);
|
|
|
} finally {
|
|
|
try {
|
|
|
if (out != null) {
|
|
|
out.close();
|
|
|
}
|
|
|
if (in != null) {
|
|
@ -119,7 +119,7 @@ public class HttpUtil {
|
|
|
|
|
|
/**
|
|
|
* 消息推送
|
|
|
*
|
|
|
*
|
|
|
* @param receiver 消息接收人
|
|
|
* @param msgType 消息类型
|
|
|
* @param title 消息标题
|
|
@ -185,9 +185,62 @@ public class HttpUtil {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送消息到websocket服务器,然后由websocket服务器中转给微信端
|
|
|
* @param userid 接收数据的患者id
|
|
|
* @param data 内容
|
|
|
* @return 推送成功:{"errno":"0","errmsg":""},推送失败:{"errno":"1","errmsg":"User is not online"}
|
|
|
* @author shenzaixin
|
|
|
*/
|
|
|
public static String sendWeixinWebsocketMsg(String userid,String data){
|
|
|
PrintWriter out = null;
|
|
|
BufferedReader in = null;
|
|
|
HttpURLConnection conn = null;
|
|
|
try {
|
|
|
String url = SystemConf.getInstance().getWeixinWebsocketServer() + "?userid=" + userid + "&data=" + data;
|
|
|
URL realUrl = new URL(url);
|
|
|
// 打开和URL之间的连接
|
|
|
conn = (HttpURLConnection) realUrl.openConnection();
|
|
|
conn.setRequestMethod("GET");
|
|
|
conn.setDoOutput(true);
|
|
|
conn.setDoInput(true);
|
|
|
conn.setUseCaches(false);
|
|
|
conn.setRequestProperty("Content-Type", "application/json");
|
|
|
// OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
|
|
|
// osw.write(param.toString());
|
|
|
// osw.flush();
|
|
|
|
|
|
// 读取返回内容
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
|
String temp;
|
|
|
while ((temp = br.readLine()) != null) {
|
|
|
buffer.append(temp);
|
|
|
}
|
|
|
System.out.println(buffer.toString());
|
|
|
JSONObject json = new JSONObject(buffer.toString());
|
|
|
return json.toString();
|
|
|
} catch (Exception e) {
|
|
|
logger.error("push message error:", e);
|
|
|
return "{\"errno\":\"1\",\"errmsg\":\""+e.getMessage()+"\"}";
|
|
|
} finally {
|
|
|
try {
|
|
|
if (out != null) {
|
|
|
out.close();
|
|
|
}
|
|
|
if (in != null) {
|
|
|
in.close();
|
|
|
}
|
|
|
} catch (IOException ex) {
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 向指定 URL 发送POST方法的请求
|
|
|
*
|
|
|
*
|
|
|
* @param url 发送请求的 URL带上参数
|
|
|
* @param param POST参数。
|
|
|
* @param charset 编码格式
|