InvokeMethod.cs 18 KB

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