InvokeMethod.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Reflection;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using MedicalInsurance.Helper;
  13. namespace MedicalInsurance.Common
  14. {
  15. class InvokeMethod
  16. {
  17. public string Post(string Url, string jsonParas)
  18. {
  19. GlobalVariables.writeLog("URL:" + Url);
  20. GlobalVariables.writeLog("jsonParas:" + jsonParas);
  21. string strURL = Url;
  22. //创建一个HTTP请求
  23. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
  24. //Post请求方式
  25. request.Method = "POST";
  26. //内容类型
  27. request.ContentType = "application/json";
  28. //设置参数,并进行URL编码
  29. string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas);
  30. byte[] payload;
  31. //将Json字符串转化为字节
  32. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  33. //设置请求的ContentLength
  34. request.ContentLength = payload.Length;
  35. //发送请求,获得请求流
  36. Stream writer;
  37. try
  38. {
  39. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  40. }
  41. catch (Exception)
  42. {
  43. writer = null;
  44. GlobalVariables.writeLog("连接服务器失败!");
  45. }
  46. //将请求参数写入流
  47. writer.Write(payload, 0, payload.Length);
  48. writer.Close();//关闭请求流
  49. // String strValue = "";//strValue为http响应所返回的字符流
  50. HttpWebResponse response;
  51. try
  52. {
  53. //获得响应流
  54. response = (HttpWebResponse)request.GetResponse();
  55. }
  56. catch (WebException ex)
  57. {
  58. response = ex.Response as HttpWebResponse;
  59. }
  60. Stream s = response.GetResponseStream();
  61. // Stream postData = Request.InputStream;
  62. StreamReader sRead = new StreamReader(s);
  63. string postContent = sRead.ReadToEnd();
  64. sRead.Close();
  65. //解析postContent,插入医保交易日志表
  66. IrisServices iris = new IrisServices();
  67. dynamic joIris = new JObject();
  68. JObject joTmp = new JObject();
  69. joTmp.Add("inParam", jsonParas);
  70. joTmp.Add("outParam", postContent);
  71. joIris.code = "09010021";
  72. joIris.HospitalID = GlobalVariables.hospitalDr;
  73. joIris.InterfaceID = GlobalVariables.InterfaceID;
  74. joIris.Add("params", JObject.FromObject(joTmp));
  75. iris.Invoke(joIris.ToString());
  76. return postContent;//返回Json数据
  77. }
  78. public bool Download(string url, string strParams)
  79. {
  80. try
  81. {
  82. GlobalVariables.writeLog("strParams" + strParams);
  83. JObject jsonInParam = JObject.Parse(strParams);
  84. string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];
  85. GlobalVariables.writeLog("filename" + fileName);
  86. string filePath = System.Environment.CurrentDirectory + "\\" + fileName;
  87. if (File.Exists(filePath))
  88. {
  89. File.Delete(filePath);
  90. }
  91. FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  92. string strURL = url;
  93. //创建一个HTTP请求
  94. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
  95. //Post请求方式
  96. request.Method = "POST";
  97. //内容类型
  98. request.ContentType = "application/json";
  99. //设置参数,并进行URL编码
  100. string paraUrlCoded = strParams;//System.Web.HttpUtility.UrlEncode(jsonParas);
  101. byte[] payload;
  102. //将Json字符串转化为字节
  103. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  104. //设置请求的ContentLength
  105. request.ContentLength = payload.Length;
  106. Stream writer;
  107. try
  108. {
  109. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  110. }
  111. catch (Exception)
  112. {
  113. writer = null;
  114. Console.Write("连接服务器失败!");
  115. }
  116. //将请求参数写入流
  117. writer.Write(payload, 0, payload.Length);
  118. writer.Close();//关闭请求流
  119. // String strValue = "";//strValue为http响应所返回的字符流
  120. //发送请求并获取相应回应数据
  121. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  122. //直到request.GetResponse()程序才开始向目标网页发送Post请求
  123. Stream responseStream = response.GetResponseStream();
  124. //创建本地文件写入流
  125. byte[] bArr = new byte[1024];
  126. int iTotalSize = 0;
  127. int size = responseStream.Read(bArr, 0, (int)bArr.Length);
  128. while (size > 0)
  129. {
  130. iTotalSize += size;
  131. fs.Write(bArr, 0, size);
  132. size = responseStream.Read(bArr, 0, (int)bArr.Length);
  133. }
  134. fs.Close();
  135. responseStream.Close();
  136. return true;
  137. }
  138. catch (Exception ex)
  139. {
  140. return false;
  141. }
  142. }
  143. }
  144. class IrisServices
  145. {
  146. public string Invoke(string data)
  147. {
  148. GlobalVariables.writeLog("入参:" + data);
  149. //先根据用户请求的uri构造请求地址
  150. string url = string.Format("{0}/{1}", GlobalVariables.irisServiceIP, GlobalVariables.irisServiceURL);
  151. //log.Write(serviceUrl);
  152. //log.Write(data);
  153. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  154. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  155. //创建Web访问对象
  156. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  157. //
  158. //把用户传过来的数据转成“UTF-8”的字节流
  159. byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
  160. myRequest.Method = "POST";
  161. myRequest.ContentLength = buf.Length;
  162. myRequest.ContentType = "application/json";
  163. //myRequest.Headers.Add("Authorization", hisauthorization);
  164. //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");
  165. //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");
  166. myRequest.Headers.Add("Authorization", GlobalVariables.irisServiceAuthorization);
  167. myRequest.MaximumAutomaticRedirections = 1;
  168. myRequest.AllowAutoRedirect = true;
  169. //发送请求
  170. Stream stream = myRequest.GetRequestStream();
  171. stream.Write(buf, 0, buf.Length);
  172. stream.Close();
  173. //获取接口返回值
  174. //通过Web访问对象获取响应内容
  175. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  176. //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
  177. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  178. //string rtn = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
  179. string rtn = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
  180. reader.Close();
  181. myResponse.Close();
  182. GlobalVariables.writeLog("出参:" + rtn);
  183. return rtn;
  184. }
  185. }
  186. class YinHaiCom
  187. {
  188. string progID1 = "YinHai.CHS.InterfaceSCS";
  189. System.Type YinHaiComType;
  190. object YinHaiComInstance;
  191. //public YinHaiCom()
  192. //{
  193. // // 与指定 ProgID 关联的类型,即获取相应的Com对象
  194. // if (YinHaiComType == null)
  195. // {
  196. // GlobalVariables.writeLog("开始创建银海Com对象");
  197. // YinHaiComType = System.Type.GetTypeFromProgID(progID);
  198. // Init();
  199. // }
  200. // else
  201. // {
  202. // GlobalVariables.writeLog("银海Com对象已存在");
  203. // }
  204. //}
  205. public int Init()
  206. {
  207. YinHaiComType = System.Type.GetTypeFromProgID(progID1);
  208. // 创建Com的实例
  209. if (YinHaiComType != null)
  210. {
  211. GlobalVariables.writeLog("开始COM组件Init");
  212. //创建实例
  213. YinHaiComInstance = Activator.CreateInstance(YinHaiComType);
  214. if (YinHaiComInstance != null)
  215. {
  216. GlobalVariables.writeLog("Init实例创建成功");
  217. }
  218. //设置需要设置的参数值
  219. object[] ParamArray = new object[2];
  220. ParamArray[0] = 0;
  221. ParamArray[1] = "";
  222. ParameterModifier[] ParamMods = new ParameterModifier[1];
  223. ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数
  224. ParamMods[0][0] = true;
  225. ParamMods[0][1] = true; // 设置第二个参数为返回参数,调用含有ParameterModifier数组的重载函数
  226. YinHaiComType.InvokeMember("yh_CHS_init", // 接口函数名
  227. BindingFlags.Default | BindingFlags.InvokeMethod,
  228. null,
  229. YinHaiComInstance, // 调用的COM组件
  230. ParamArray, // 参数数组
  231. ParamMods, // 指定返回参数的ParameterModifier数组
  232. null,
  233. null);
  234. string Msg = "加载成功:" + ParamArray[1].ToString();
  235. GlobalVariables.writeLog(Msg + "___" + ParamArray[0].ToString());
  236. return (int)ParamArray[0];
  237. }
  238. else
  239. {
  240. string Msg = "YinHaiComType加载失败!";
  241. GlobalVariables.writeLog(Msg);
  242. return 1;
  243. }
  244. }
  245. public void Call(string infno,string input,out string output)
  246. {
  247. if (YinHaiComType != null)
  248. {
  249. //创建实例,不能再次创建,否则会提示没有初始化
  250. //YinHaiComInstance = Activator.CreateInstance(YinHaiComType);
  251. if (YinHaiComInstance != null)
  252. {
  253. GlobalVariables.writeLog("实例创建成功,准备调用Call服务");
  254. }
  255. else
  256. {
  257. output = "实例不存在!";
  258. GlobalVariables.writeLog("实例不存在");
  259. return;
  260. }
  261. GlobalVariables.writeLog("入参infno:" + infno);
  262. GlobalVariables.writeLog("入参input:" + input);
  263. //设置需要设置的参数值
  264. object[] ParamArray = new object[3];
  265. ParamArray[0] = infno;
  266. ParamArray[1] = input;
  267. ParamArray[2] = "";
  268. ParameterModifier[] ParamMods = new ParameterModifier[1];
  269. ParamMods[0] = new ParameterModifier(3); // 初始化为接口参数的个数
  270. //ParamMods[0][0] = false;
  271. //ParamMods[0][1] = false;
  272. ParamMods[0][2] = true;
  273. YinHaiComType.InvokeMember("yh_CHS_call", // 接口函数名
  274. BindingFlags.Default | BindingFlags.InvokeMethod,
  275. null,
  276. YinHaiComInstance, // 调用的COM组件
  277. ParamArray, // 参数数组
  278. ParamMods, // 指定返回参数的ParameterModifier数组
  279. null,
  280. null);
  281. output = ParamArray[2].ToString();
  282. }
  283. else
  284. {
  285. output = "COM加载失败!";
  286. GlobalVariables.writeLog("COM加载失败!");
  287. }
  288. // if (YinHaiComType == null)
  289. // {
  290. // output = "未找到组件,请先调用ComInit或者检查运行环境!";
  291. // GlobalVariables.writeLog(output);
  292. // return ;
  293. // }
  294. // //设置需要设置的参数值
  295. // object[] ParamArray = new object[3];
  296. // ParamArray[0] = infno;
  297. // ParamArray[1] = input;
  298. // ParamArray[2] = "";
  299. // ParameterModifier[] ParamMods = new ParameterModifier[1];
  300. // ParamMods[0] = new ParameterModifier(3); // 初始化为接口参数的个数
  301. // ParamMods[0][2] = true; // 设置第二个参数为返回参数
  302. // //调用含有ParameterModifier数组的重载函数
  303. // YinHaiComType.InvokeMember("yh_CHS_call", // 接口函数名
  304. // BindingFlags.Default | BindingFlags.InvokeMethod,
  305. // null,
  306. // YinHaiComInstance, // 调用的COM组件
  307. // ParamArray, // 参数数组
  308. // ParamMods, // 指定返回参数的ParameterModifier数组
  309. // null,
  310. //null);
  311. // output = ParamArray[2].ToString();
  312. }
  313. public void Destroy(out string output)
  314. {
  315. // 创建Com的实例
  316. if (YinHaiComType != null)
  317. {
  318. //创建实例
  319. YinHaiComInstance = Activator.CreateInstance(YinHaiComType);
  320. if (YinHaiComInstance != null)
  321. {
  322. GlobalVariables.writeLog("实例创建成功,准备调用Call服务");
  323. }
  324. else
  325. {
  326. output = "实例不存在!";
  327. GlobalVariables.writeLog("实例不存在");
  328. return;
  329. }
  330. //设置需要设置的参数值
  331. object[] ParamArray = new object[0];
  332. ParameterModifier[] ParamMods = new ParameterModifier[0];
  333. ParamMods[0] = new ParameterModifier(0); // 初始化为接口参数的个数
  334. YinHaiComType.InvokeMember("yh_CHS_destroy", // 接口函数名
  335. BindingFlags.Default | BindingFlags.InvokeMethod,
  336. null,
  337. YinHaiComInstance, // 调用的COM组件
  338. ParamArray, // 参数数组
  339. ParamMods, // 指定返回参数的ParameterModifier数组
  340. null,
  341. null);
  342. output = "destroy成功!";
  343. }
  344. else
  345. {
  346. output = "COM加载失败!";
  347. GlobalVariables.writeLog("COM加载失败!");
  348. }
  349. }
  350. }
  351. #region
  352. class DllInvoke
  353. {
  354. public static object ManagedInvoke(string DllFileName, string NameSpace, string ClassName, string MethodName, object[] ObjArrayParams)
  355. {
  356. //GlobalVariables.writeLog("程序集信息:" + NameSpace + "." + ClassName + "." + MethodName);
  357. //GlobalVariables.writeLog("文件全路径:" + DllFileName);
  358. //GlobalVariables.writeLog("命名空间.类.方法:" + NameSpace + "." + ClassName + "." + MethodName);
  359. //GlobalVariables.writeLog("入参:" + ObjArrayParams[0].ToString());
  360. object o = new object();
  361. try
  362. {
  363. //载入程序集
  364. Assembly DllAssembly = Assembly.LoadFile(DllFileName);
  365. //MessageBox.Show(NameSpace + ":" + ClassName);
  366. Type DllType = DllAssembly.GetType(NameSpace + "." + ClassName);
  367. if (DllType != null)
  368. {
  369. //MessageBox.Show("32");
  370. }
  371. //查找要调用的方 法并进行调用
  372. MethodInfo MyMethod = DllType.GetMethod(MethodName);
  373. if (MyMethod != null)
  374. {
  375. object mObject = Activator.CreateInstance(DllType);
  376. try
  377. {
  378. if (ObjArrayParams == null)
  379. {
  380. o = MyMethod.Invoke(mObject, null);
  381. }
  382. else
  383. {
  384. o = MyMethod.Invoke(mObject, new object[] { ObjArrayParams[0].ToString() });
  385. }
  386. }
  387. catch (Exception e)
  388. {
  389. string inMes ="", outMes="";
  390. if (e.InnerException != null)
  391. { inMes = e.InnerException.Message; }
  392. outMes = e.Message;
  393. GlobalVariables.writeLog("inMes:" + inMes.ToString());
  394. GlobalVariables.writeLog("outMes:" + outMes.ToString());
  395. }
  396. }
  397. }
  398. catch (Exception mEx)
  399. {
  400. string inMes = "", outMes = "";
  401. if (mEx.InnerException != null)
  402. { inMes = mEx.InnerException.Message; }
  403. GlobalVariables.writeLog("inMes:" + inMes.ToString());
  404. o = mEx.Message;
  405. }
  406. //GlobalVariables.writeLog("出参:" + o.ToString());
  407. return o;
  408. }
  409. }
  410. //银海加解密
  411. class YinHaiEncrypt
  412. {
  413. //加密
  414. [DllImport("hsafsiyhsafe.dll", CharSet = CharSet.Ansi, EntryPoint = "gm_ecb_encrypt_key@16", CallingConvention = CallingConvention.StdCall)]
  415. private static extern int gm_ecb_encrypt_key(byte[] pub_key, byte[] plain, int plain_len, byte[] cipher);
  416. //解密
  417. [DllImport("hsafsiyhsafe.dll", CharSet = CharSet.Ansi, EntryPoint = "gm_ecb_decrypt_key@16", CallingConvention = CallingConvention.StdCall)]
  418. private static extern int gm_ecb_decrypt_key(byte[] pub_key, byte[] cipher, int cipher_len, byte[] plain);
  419. //生成签名
  420. [DllImport("hsafsitool.dll", EntryPoint = "gm_sign_key@24", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
  421. private static extern int gm_sign_key(byte[] userid, byte[] prv_key, byte[] pub_key, byte[] msg, int msg_len, byte[] singrs);
  422. //验签
  423. [DllImport("hsafsitool.dll", EntryPoint = "gm_verify_key@20", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
  424. private static extern int gm_verify_key(byte[] userid, byte[] pub_key, byte[] msg, int msg_len, byte[] singrs);
  425. public const string pubKey = "725871EF03A7AA708708615A0B9400C14A9E7E4F0E5E14E911AE39CD9177C17685DED3BBDB1B242F09393BA7A0C852338A0D06D1619B79B70CF3CCD3754FF646";
  426. public const string prvKey = "BC5E29E032BF908DEB6EB180A0C61ED7F9AA7BF56C35B727A85CD65F79504384";
  427. public string Signature(string data)
  428. {
  429. string rtn = "";
  430. try
  431. {
  432. byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);
  433. byte[] prv_key = Encoding.UTF8.GetBytes(prvKey);
  434. byte[] userid = Encoding.UTF8.GetBytes("");
  435. byte[] msg = Encoding.UTF8.GetBytes(data);
  436. byte[] singrs = new byte[1024];
  437. if (gm_sign_key(userid, prv_key, pub_key, msg, msg.Length, singrs) > 0)
  438. {
  439. rtn = Encoding.UTF8.GetString(singrs).Replace("\u0000", "").Trim();
  440. }
  441. else
  442. {
  443. rtn = "";
  444. }
  445. return rtn;
  446. }
  447. catch (Exception ex)
  448. {
  449. rtn = "" + ex.Message;
  450. return rtn;
  451. }
  452. finally
  453. {
  454. GlobalVariables.writeLog("",data,rtn);
  455. }
  456. }
  457. public int VerifySignature(string plain, string signature)
  458. {
  459. byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);
  460. byte[] prv_key = Encoding.UTF8.GetBytes(prvKey);
  461. byte[] userid = Encoding.UTF8.GetBytes("");
  462. byte[] msg = Encoding.UTF8.GetBytes(plain);
  463. byte[] singrs = Encoding.UTF8.GetBytes(signature);
  464. int result = gm_verify_key(userid, pub_key, msg, msg.Length, singrs);
  465. return result;
  466. }
  467. public string Encrypt(string plain)
  468. {
  469. byte[] plainB = Encoding.UTF8.GetBytes(plain);
  470. byte[] cipher = new byte[(plainB.Length + 2048)];
  471. byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);
  472. GlobalVariables.writeLog("Encrypt明文:'" + plain + "'");
  473. if (gm_ecb_encrypt_key(pub_key, plainB, plainB.Length , cipher) > 0)
  474. {
  475. string result = Encoding.UTF8.GetString(cipher).Replace("\u0000", "").Trim();
  476. GlobalVariables.writeLog("Encrypt密文:" + result);
  477. return result;
  478. }
  479. else
  480. {
  481. return "";
  482. }
  483. }
  484. public string Decrypt(string cipher)
  485. {
  486. byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);
  487. byte[] cipherB = Encoding.UTF8.GetBytes(cipher);
  488. byte[] plain = new byte[(cipherB.Length)];
  489. if (gm_ecb_decrypt_key(pub_key, cipherB, cipher.Length, plain) > 0)
  490. {
  491. return Encoding.UTF8.GetString(plain);
  492. }
  493. else
  494. {
  495. return "";
  496. }
  497. }
  498. }
  499. #endregion
  500. }