InvokeMethod.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.IO;
  4. using System.Net;
  5. using System.Reflection;
  6. using System.Text;
  7. using MedicalInsurance.Common;
  8. namespace ChengDuMedInsu2
  9. {
  10. class CenterServices
  11. {
  12. public string Post(string Url, string jsonParas)
  13. {
  14. GlobalVariables.writeLog("CenterServices.URL:" + Url);
  15. //GlobalVariables.writeLog("CenterServices.post入参:" + jsonParas);
  16. string strURL = Url;
  17. //创建一个HTTP请求
  18. GlobalVariables.writeLog(strURL);
  19. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
  20. GlobalVariables.writeLog("111");
  21. //Post请求方式
  22. request.Method = "POST";
  23. //内容类型
  24. request.ContentType = "application/json";
  25. //设置参数,并进行URL编码
  26. string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas);
  27. byte[] payload;
  28. //将Json字符串转化为字节
  29. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  30. //设置请求的ContentLength
  31. request.ContentLength = payload.Length;
  32. //发送请求,获得请求流
  33. Stream writer;
  34. try
  35. {
  36. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  37. }
  38. catch (Exception)
  39. {
  40. writer = null;
  41. GlobalVariables.writeLog("连接服务器失败!");
  42. }
  43. //将请求参数写入流
  44. writer.Write(payload, 0, payload.Length);
  45. writer.Close();//关闭请求流
  46. // String strValue = "";//strValue为http响应所返回的字符流
  47. HttpWebResponse response;
  48. try
  49. {
  50. //获得响应流
  51. response = (HttpWebResponse)request.GetResponse();
  52. }
  53. catch (WebException ex)
  54. {
  55. response = ex.Response as HttpWebResponse;
  56. }
  57. Stream s = response.GetResponseStream();
  58. // Stream postData = Request.InputStream;
  59. StreamReader sRead = new StreamReader(s);
  60. string postContent = sRead.ReadToEnd();
  61. //GlobalVariables.writeLog("CenterServices.post出参:" + postContent);
  62. sRead.Close();
  63. //解析postContent,插入医保交易日志表
  64. //IrisServices iris = new IrisServices();
  65. //dynamic joIris = new JObject();
  66. //JObject joinParam = new JObject();
  67. //joinParam = JObject.Parse(jsonParas);
  68. //JObject jooutParam = new JObject();
  69. //jooutParam = JObject.Parse(postContent);
  70. //joIris.Add("inParam", JObject.FromObject(joinParam));
  71. //joIris.Add("outParam", JObject.FromObject(jooutParam));
  72. //joIris.code = "09010021";
  73. //joIris.HospitalID = GlobalVariables.hospitalID;
  74. //joIris.InterfaceID = GlobalVariables.InterfaceID;
  75. //iris.Invoke(joIris.ToString());
  76. return postContent;//返回Json数据
  77. }
  78. public string Download(string url, string strParams)
  79. {
  80. string error = string.Empty; int errorCode = 0;
  81. try
  82. {
  83. GlobalVariables.writeLog("CenterServices.Download入参:" + strParams);
  84. JObject jsonInParam = JObject.Parse(strParams);
  85. string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];
  86. string filePath = System.Environment.CurrentDirectory +"\\Download\\" + fileName;
  87. //如果不存在目录,则创建目录
  88. if (!Directory.Exists(System.Environment.CurrentDirectory + "\\Download"))
  89. {
  90. //创建文件夹
  91. DirectoryInfo dirInfo = Directory.CreateDirectory(System.Environment.CurrentDirectory + "\\Download");
  92. }
  93. if (File.Exists(filePath))
  94. {
  95. File.Delete(filePath);
  96. }
  97. FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  98. string strURL = url;
  99. //创建一个HTTP请求
  100. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
  101. //Post请求方式
  102. request.Method = "POST";
  103. //内容类型
  104. request.ContentType = "application/json";
  105. //设置参数,并进行URL编码
  106. string paraUrlCoded = strParams;//System.Web.HttpUtility.UrlEncode(jsonParas);
  107. byte[] payload;
  108. //将Json字符串转化为字节
  109. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  110. //设置请求的ContentLength
  111. request.ContentLength = payload.Length;
  112. Stream writer;
  113. try
  114. {
  115. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  116. }
  117. catch (Exception)
  118. {
  119. writer = null;
  120. errorCode = -100;
  121. error ="连接服务器失败!";
  122. }
  123. //将请求参数写入流
  124. writer.Write(payload, 0, payload.Length);
  125. writer.Close();//关闭请求流
  126. // String strValue = "";//strValue为http响应所返回的字符流
  127. //发送请求并获取相应回应数据
  128. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  129. //直到request.GetResponse()程序才开始向目标网页发送Post请求
  130. Stream responseStream = response.GetResponseStream();
  131. //创建本地文件写入流
  132. byte[] bArr = new byte[1024];
  133. int iTotalSize = 0;
  134. int size = responseStream.Read(bArr, 0, (int)bArr.Length);
  135. while (size > 0)
  136. {
  137. iTotalSize += size;
  138. fs.Write(bArr, 0, size);
  139. size = responseStream.Read(bArr, 0, (int)bArr.Length);
  140. }
  141. fs.Close();
  142. responseStream.Close();
  143. dynamic joReturn = new JObject();
  144. joReturn.errorCode = errorCode;
  145. joReturn.error = error;
  146. joReturn.filePath = filePath;
  147. GlobalVariables.writeLog("CenterServices.Download出参:" + joReturn.ToString());
  148. return joReturn.ToString();
  149. }
  150. catch (Exception ex)
  151. {
  152. errorCode = -100;
  153. error = ex.Message;
  154. dynamic joReturn = new JObject();
  155. joReturn.errorCode = errorCode;
  156. joReturn.error = error;
  157. GlobalVariables.writeLog("CenterServices.Download出参:" + joReturn.ToString());
  158. return joReturn.ToString();
  159. }
  160. }
  161. }
  162. class IrisServices
  163. {
  164. public string Invoke(string data)
  165. {
  166. //GlobalVariables.writeLog("IrisServices.Invoke入参:" + data);
  167. //先根据用户请求的uri构造请求地址
  168. string url = string.Format("{0}/{1}", GlobalVariables.irisServiceIP, GlobalVariables.irisServiceURL);
  169. //log.Write(serviceUrl);
  170. //log.Write(data);
  171. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  172. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  173. //创建Web访问对象
  174. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  175. //
  176. //把用户传过来的数据转成“UTF-8”的字节流
  177. byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
  178. myRequest.Method = "POST";
  179. myRequest.ContentLength = buf.Length;
  180. myRequest.ContentType = "application/json";
  181. //myRequest.Headers.Add("Authorization", hisauthorization);
  182. //myRequest.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
  183. //myRequest.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36");
  184. myRequest.Headers.Add("Authorization", GlobalVariables.irisServiceAuthorization);
  185. myRequest.MaximumAutomaticRedirections = 1;
  186. myRequest.AllowAutoRedirect = true;
  187. //发送请求
  188. Stream stream = myRequest.GetRequestStream();
  189. stream.Write(buf, 0, buf.Length);
  190. stream.Close();
  191. //获取接口返回值
  192. //通过Web访问对象获取响应内容
  193. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  194. //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
  195. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  196. //string rtn = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
  197. string rtn = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
  198. reader.Close();
  199. myResponse.Close();
  200. //GlobalVariables.writeLog("IrisServices.Invoke出参:" + rtn);
  201. return rtn;
  202. }
  203. }
  204. class YinHaiCom
  205. {
  206. string progID1 = "YinHai.CHS.InterfaceSCS";
  207. System.Type YinHaiComType;
  208. object YinHaiComInstance;
  209. //public YinHaiCom()
  210. //{
  211. // // 与指定 ProgID 关联的类型,即获取相应的Com对象
  212. // if (YinHaiComType == null)
  213. // {
  214. // GlobalVariables.writeLog("开始创建银海Com对象");
  215. // YinHaiComType = System.Type.GetTypeFromProgID(progID);
  216. // Init();
  217. // }
  218. // else
  219. // {
  220. // GlobalVariables.writeLog("银海Com对象已存在");
  221. // }
  222. //}
  223. public int Init()
  224. {
  225. YinHaiComType = System.Type.GetTypeFromProgID(progID1);
  226. // 创建Com的实例
  227. if (YinHaiComType != null)
  228. {
  229. GlobalVariables.writeLog("开始COM组件Init");
  230. //创建实例
  231. YinHaiComInstance = Activator.CreateInstance(YinHaiComType);
  232. if (YinHaiComInstance != null)
  233. {
  234. GlobalVariables.writeLog("Init实例创建成功");
  235. }
  236. //设置需要设置的参数值
  237. object[] ParamArray = new object[2];
  238. ParamArray[0] = 0;
  239. ParamArray[1] = "";
  240. ParameterModifier[] ParamMods = new ParameterModifier[1];
  241. ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数
  242. ParamMods[0][0] = true;
  243. ParamMods[0][1] = true; // 设置第二个参数为返回参数,调用含有ParameterModifier数组的重载函数
  244. YinHaiComType.InvokeMember("yh_CHS_init", // 接口函数名
  245. BindingFlags.Default | BindingFlags.InvokeMethod,
  246. null,
  247. YinHaiComInstance, // 调用的COM组件
  248. ParamArray, // 参数数组
  249. ParamMods, // 指定返回参数的ParameterModifier数组
  250. null,
  251. null);
  252. string Msg = "加载成功:" + ParamArray[1].ToString();
  253. GlobalVariables.writeLog(Msg + "___" + ParamArray[0].ToString());
  254. return (int)ParamArray[0];
  255. }
  256. else
  257. {
  258. string Msg = "YinHaiComType加载失败!";
  259. GlobalVariables.writeLog(Msg);
  260. return 1;
  261. }
  262. }
  263. public void Call(string infno,string input,out string output)
  264. {
  265. try
  266. {
  267. if (YinHaiComType != null)
  268. {
  269. //创建实例,不能再次创建,否则会提示没有初始化
  270. //YinHaiComInstance = Activator.CreateInstance(YinHaiComType);
  271. if (YinHaiComInstance != null)
  272. {
  273. GlobalVariables.writeLog("实例创建成功,准备调用Call服务");
  274. }
  275. else
  276. {
  277. output = "实例不存在!";
  278. GlobalVariables.writeLog("实例不存在");
  279. return;
  280. }
  281. //GlobalVariables.writeLog("入参infno:" + infno);
  282. //GlobalVariables.writeLog("入参input:" + input);
  283. //设置需要设置的参数值
  284. object[] ParamArray = new object[3];
  285. ParamArray[0] = infno;
  286. ParamArray[1] = input;
  287. ParamArray[2] = "";
  288. ParameterModifier[] ParamMods = new ParameterModifier[1];
  289. ParamMods[0] = new ParameterModifier(3); // 初始化为接口参数的个数
  290. //ParamMods[0][0] = false;
  291. //ParamMods[0][1] = false;
  292. ParamMods[0][2] = true;
  293. YinHaiComType.InvokeMember("yh_CHS_call", // 接口函数名
  294. BindingFlags.Default | BindingFlags.InvokeMethod,
  295. null,
  296. YinHaiComInstance, // 调用的COM组件
  297. ParamArray, // 参数数组
  298. ParamMods, // 指定返回参数的ParameterModifier数组
  299. null,
  300. null);
  301. output = ParamArray[2].ToString();
  302. GlobalVariables.writeLog("Com输出:" + output);
  303. }
  304. else
  305. {
  306. output = "COM加载失败!";
  307. GlobalVariables.writeLog("COM加载失败!");
  308. }
  309. }
  310. catch (Exception ex)
  311. {
  312. output = ex.Message;
  313. GlobalVariables.writeLog("COM加载失败!"+ output);
  314. }
  315. // if (YinHaiComType == null)
  316. // {
  317. // output = "未找到组件,请先调用ComInit或者检查运行环境!";
  318. // GlobalVariables.writeLog(output);
  319. // return ;
  320. // }
  321. // //设置需要设置的参数值
  322. // object[] ParamArray = new object[3];
  323. // ParamArray[0] = infno;
  324. // ParamArray[1] = input;
  325. // ParamArray[2] = "";
  326. // ParameterModifier[] ParamMods = new ParameterModifier[1];
  327. // ParamMods[0] = new ParameterModifier(3); // 初始化为接口参数的个数
  328. // ParamMods[0][2] = true; // 设置第二个参数为返回参数
  329. // //调用含有ParameterModifier数组的重载函数
  330. // YinHaiComType.InvokeMember("yh_CHS_call", // 接口函数名
  331. // BindingFlags.Default | BindingFlags.InvokeMethod,
  332. // null,
  333. // YinHaiComInstance, // 调用的COM组件
  334. // ParamArray, // 参数数组
  335. // ParamMods, // 指定返回参数的ParameterModifier数组
  336. // null,
  337. //null);
  338. // output = ParamArray[2].ToString();
  339. }
  340. public void Print(string input, out string output)
  341. {
  342. if (YinHaiComType != null)
  343. {
  344. //创建实例,不能再次创建,否则会提示没有初始化
  345. //YinHaiComInstance = Activator.CreateInstance(YinHaiComType);
  346. if (YinHaiComInstance != null)
  347. {
  348. GlobalVariables.writeLog("实例创建成功,准备调用Call服务");
  349. }
  350. else
  351. {
  352. output = "实例不存在!";
  353. GlobalVariables.writeLog("实例不存在");
  354. return;
  355. }
  356. //GlobalVariables.writeLog("入参infno:" + infno);
  357. //GlobalVariables.writeLog("入参input:" + input);
  358. //设置需要设置的参数值
  359. object[] ParamArray = new object[2];
  360. ParamArray[0] = input;
  361. ParamArray[1] = "";
  362. ParameterModifier[] ParamMods = new ParameterModifier[1];
  363. ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数
  364. //ParamMods[0][0] = false;
  365. //ParamMods[0][1] = false;
  366. ParamMods[0][1] = true;
  367. YinHaiComType.InvokeMember("yh_CHS_print", // 接口函数名
  368. BindingFlags.Default | BindingFlags.InvokeMethod,
  369. null,
  370. YinHaiComInstance, // 调用的COM组件
  371. ParamArray, // 参数数组
  372. ParamMods, // 指定返回参数的ParameterModifier数组
  373. null,
  374. null);
  375. output = ParamArray[1].ToString();
  376. }
  377. else
  378. {
  379. output = "COM加载失败!";
  380. GlobalVariables.writeLog("COM加载失败!");
  381. }
  382. // if (YinHaiComType == null)
  383. // {
  384. // output = "未找到组件,请先调用ComInit或者检查运行环境!";
  385. // GlobalVariables.writeLog(output);
  386. // return ;
  387. // }
  388. // //设置需要设置的参数值
  389. // object[] ParamArray = new object[3];
  390. // ParamArray[0] = infno;
  391. // ParamArray[1] = input;
  392. // ParamArray[2] = "";
  393. // ParameterModifier[] ParamMods = new ParameterModifier[1];
  394. // ParamMods[0] = new ParameterModifier(3); // 初始化为接口参数的个数
  395. // ParamMods[0][2] = true; // 设置第二个参数为返回参数
  396. // //调用含有ParameterModifier数组的重载函数
  397. // YinHaiComType.InvokeMember("yh_CHS_call", // 接口函数名
  398. // BindingFlags.Default | BindingFlags.InvokeMethod,
  399. // null,
  400. // YinHaiComInstance, // 调用的COM组件
  401. // ParamArray, // 参数数组
  402. // ParamMods, // 指定返回参数的ParameterModifier数组
  403. // null,
  404. //null);
  405. // output = ParamArray[2].ToString();
  406. }
  407. public void Destroy(out string output)
  408. {
  409. // 创建Com的实例
  410. if (YinHaiComType != null)
  411. {
  412. //创建实例
  413. YinHaiComInstance = Activator.CreateInstance(YinHaiComType);
  414. if (YinHaiComInstance != null)
  415. {
  416. GlobalVariables.writeLog("实例创建成功,准备调用Call服务");
  417. }
  418. else
  419. {
  420. output = "实例不存在!";
  421. GlobalVariables.writeLog("实例不存在");
  422. return;
  423. }
  424. //设置需要设置的参数值
  425. object[] ParamArray = new object[0];
  426. ParameterModifier[] ParamMods = new ParameterModifier[0];
  427. ParamMods[0] = new ParameterModifier(0); // 初始化为接口参数的个数
  428. YinHaiComType.InvokeMember("yh_CHS_destroy", // 接口函数名
  429. BindingFlags.Default | BindingFlags.InvokeMethod,
  430. null,
  431. YinHaiComInstance, // 调用的COM组件
  432. ParamArray, // 参数数组
  433. ParamMods, // 指定返回参数的ParameterModifier数组
  434. null,
  435. null);
  436. output = "destroy成功!";
  437. }
  438. else
  439. {
  440. output = "COM加载失败!";
  441. GlobalVariables.writeLog("COM加载失败!");
  442. }
  443. }
  444. }
  445. }