发送通知:
private static String getOpenStream(String url,String xmlContent,String method) {
PostMethod postMethod=null;
String resultContent = "error";
HttpClient httpClient =null;
try {
// 构造HttpClient的实例
httpClient = new HttpClient();
// 设置连接超时
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(20000);
// 设置字符集
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");
// 创建POST方法的实例
postMethod = new PostMethod(url);
// 使用系统提供的默认的恢复策略 该策略在碰到IO异常的时候将自动重试3次。
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 20000);
NameValuePair[] nameValuePairs = new NameValuePair[3];
System.out.println("请求xml ---- "+xmlContent);
nameValuePairs[0] = new NameValuePair("resXml", xmlContent);
nameValuePairs[1] = new NameValuePair("method", method);
nameValuePairs[2] = new NameValuePair("key", Md5Util.encode(xmlContent));
postMethod.setRequestBody(nameValuePairs);
// 发送连接
int statusCode = httpClient.executeMethod(postMethod);
System.out.println(statusCode);
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Method failed: " + postMethod.getStatusLine());
} else {
resultContent=postMethod.getResponseBodyAsString();
System.out.println(resultContent);
return resultContent;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return resultContent;
}
接收通知:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("GBK");
String params = request.getParameter("resXml");
logger.debug("传入参数:"+params+";签名字符 串:"+request.getParameter("key")+";method:"+request.getParameter("method"));
String result="";
String mysign=Md5Util.encode(params);
if(mysign.equals(request.getParameter("key"))){
//调用方法
DHCAppMobileServiceSoapProxy imp=new DHCAppMobileServiceSoapProxy();
Class clazz=imp.getClass();
Method mehtod;
try {
mehtod = clazz.getMethod(request.getParameter("method"), String.class);
Object obj=mehtod.invoke(imp, params);
logger.debug("医院接口返回值:"+obj);
result= obj==null?"":obj.toString();
} catch (Exception e) {
e.printStackTrace();
result= "请求医院接口失败";
logger.error("请求医院接口失败");
}
}else{
result="请求报文被篡改";
logger.error("请求报文被篡改");
}
String md5=Md5Util.encode(result);
OutputStream out = response.getOutputStream();
out.write(("data=" + result + "&key=" + md5).getBytes());
out.flush();
out.close();
}