InvokeHelper.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /******************************************************************************
  2. * 文件名称: InvokeHelper.cs
  3. * 文件说明: 调用助手,调用方法的封装
  4. * 当前版本: V1.0
  5. * 创建日期: 2022-04-12
  6. *
  7. * 2020-04-12: 增加 businessDLLInvoke 方法
  8. * 2020-04-12: 增加 writeLog 方法
  9. * 2020-04-14: 增加 businessDLLInvoke(重载) 方法
  10. * 2020-04-14: 增加 irisServiceInvoke 方法
  11. ******************************************************************************/
  12. using Newtonsoft.Json.Linq;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Net;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows.Forms;
  21. using PTMedInsuInterface.Common;
  22. using Newtonsoft.Json;
  23. using PTMedInsuInterface.Variables;
  24. namespace PTMedInsuInterface.Helper
  25. {
  26. class InvokeHelper
  27. {
  28. private string serviceURL;
  29. private string authorization;
  30. /// <summary>
  31. /// 封装业务DLL的调用,返回JSON对象
  32. /// </summary>
  33. /// <param name="methodName"></param>
  34. /// <param name="inParam"></param>
  35. /// <returns></returns>
  36. public JObject InvokeBusinessDLL(string methodName, string inParam)
  37. {
  38. string outParam = string.Empty;
  39. try
  40. {
  41. DllInvoke dllInvoke = new DllInvoke();
  42. string path = Global.curEvt.path + "\\" + Global.dll.name;
  43. string[] str = new string[1];
  44. str[0] = inParam;
  45. object o2 = DllInvoke.ManagedInvoke(path,Global.dll.nameSpace, "InsuBusiness", methodName, str);
  46. outParam = o2.ToString();
  47. JObject joRtn = JObject.Parse(outParam);
  48. return joRtn;
  49. }
  50. catch (Exception ex)
  51. {
  52. outParam = "InvokeBusinessDLL:" + outParam + "\r\n" + ex.Message;
  53. return JsonHelper.setExceptionJson(-1, "businessDLLInvoke:" , outParam);
  54. }
  55. finally
  56. {
  57. Global.writeLog("InvokeBusinessDLL." + methodName, inParam, outParam);
  58. }
  59. }
  60. /// <summary>
  61. /// iris服务调用的封装
  62. /// </summary>
  63. /// <param name="data"></param>
  64. /// <returns></returns>
  65. public JObject invokeIrisService(string data, string serviceDesc)
  66. {
  67. string rtn = "", url = "";
  68. JObject joRtn = new JObject();
  69. try
  70. {
  71. //先根据用户请求的uri构造请求地址
  72. url = serviceURL;
  73. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  74. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  75. //创建Web访问对象
  76. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  77. //把用户传过来的数据转成“UTF-8”的字节流
  78. byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
  79. //添加头部信息
  80. myRequest.Method = "POST";
  81. myRequest.ContentLength = buf.Length;
  82. myRequest.ContentType = "application/json";
  83. myRequest.Headers.Add("Authorization", authorization);
  84. myRequest.MaximumAutomaticRedirections = 1;
  85. myRequest.AllowAutoRedirect = true;
  86. //发送请求
  87. Stream stream = myRequest.GetRequestStream();
  88. stream.Write(buf, 0, buf.Length);
  89. stream.Close();
  90. //获取接口返回值
  91. //通过Web访问对象获取响应内容
  92. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  93. //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
  94. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  95. //string rtn = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
  96. rtn = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
  97. reader.Close();
  98. myResponse.Close();
  99. joRtn = JObject.Parse(rtn);
  100. return joRtn;
  101. }
  102. catch (Exception ex)
  103. {
  104. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message );
  105. rtn = JsonConvert.SerializeObject(joRtn);
  106. return joRtn;
  107. }
  108. }
  109. /// <summary>
  110. /// HIS服务调用的封装
  111. /// </summary>
  112. /// <param name="data"></param>
  113. /// <returns></returns>
  114. public JObject invokeHISService(string data, string serviceDesc)
  115. {
  116. JObject joRtn = new JObject();
  117. try
  118. {
  119. //先根据用户请求的uri构造请求地址
  120. serviceURL = string.Format("{0}/{1}", Global.hisConfig.ip, Global.hisConfig.url);
  121. authorization = Global.hisConfig.authorization;
  122. joRtn = invokeIrisService(data, serviceDesc);
  123. return joRtn;
  124. }
  125. catch (Exception ex)
  126. {
  127. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  128. return joRtn;
  129. }
  130. finally
  131. {
  132. Global.writeLog(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), JsonHelper.Compress(joRtn));
  133. }
  134. }
  135. /// <summary>
  136. /// 医保平台服务调用的封装
  137. /// </summary>
  138. /// <param name="data"></param>
  139. /// <returns></returns>
  140. public JObject invokeInsuService(string data, string serviceDesc)
  141. {
  142. string rtn = "";
  143. JObject joRtn = new JObject();
  144. try
  145. {
  146. //先根据用户请求的uri构造请求地址
  147. serviceURL = string.Format("{0}/{1}", Global.insuConfig.ip, Global.insuConfig.url);
  148. authorization = Global.insuConfig.authorization;
  149. joRtn = invokeIrisService(data, serviceDesc);
  150. rtn = JsonConvert.SerializeObject(joRtn);
  151. return joRtn;
  152. }
  153. catch (Exception ex)
  154. {
  155. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message );
  156. rtn = JsonConvert.SerializeObject(joRtn);
  157. return joRtn;
  158. }
  159. finally
  160. {
  161. Global.writeLog(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), rtn);
  162. }
  163. }
  164. }
  165. }