123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using MedicalInsurance.Helper;
- namespace MedicalInsurance.Common
- {
- class InvokeMethod
- {
- public string Post(string Url, string jsonParas)
- {
- GlobalVariables.writeLog("URL:" + Url);
- GlobalVariables.writeLog("jsonParas:" + jsonParas);
- string strURL = Url;
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
-
- request.Method = "POST";
-
- request.ContentType = "application/json";
-
- string paraUrlCoded = jsonParas;
- byte[] payload;
-
- payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
-
- request.ContentLength = payload.Length;
-
- Stream writer;
- try
- {
- writer = request.GetRequestStream();
- }
- catch (Exception)
- {
- writer = null;
- GlobalVariables.writeLog("连接服务器失败!");
- }
-
- writer.Write(payload, 0, payload.Length);
- writer.Close();
-
- HttpWebResponse response;
- try
- {
-
- response = (HttpWebResponse)request.GetResponse();
- }
- catch (WebException ex)
- {
- response = ex.Response as HttpWebResponse;
- }
- Stream s = response.GetResponseStream();
-
- StreamReader sRead = new StreamReader(s);
- string postContent = sRead.ReadToEnd();
- sRead.Close();
-
- IrisServices iris = new IrisServices();
- dynamic joIris = new JObject();
- JObject joTmp = new JObject();
- joTmp.Add("inParam", jsonParas);
- joTmp.Add("outParam", postContent);
- joIris.code = "09010021";
- joIris.HospitalID = GlobalVariables.hospitalDr;
- joIris.InterfaceID = GlobalVariables.InterfaceID;
- joIris.Add("params", JObject.FromObject(joTmp));
- iris.Invoke(joIris.ToString());
- return postContent;
- }
- public bool Download(string url, string strParams)
- {
- try
- {
- GlobalVariables.writeLog("strParams" + strParams);
- JObject jsonInParam = JObject.Parse(strParams);
- string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];
- GlobalVariables.writeLog("filename" + fileName);
- string filePath = System.Environment.CurrentDirectory + "\\" + fileName;
- if (File.Exists(filePath))
- {
- File.Delete(filePath);
- }
- FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
- string strURL = url;
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
-
- request.Method = "POST";
-
- request.ContentType = "application/json";
-
- string paraUrlCoded = strParams;
- byte[] payload;
-
- payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
-
- request.ContentLength = payload.Length;
- Stream writer;
- try
- {
- writer = request.GetRequestStream();
- }
- catch (Exception)
- {
- writer = null;
- Console.Write("连接服务器失败!");
- }
-
- writer.Write(payload, 0, payload.Length);
- writer.Close();
-
-
- HttpWebResponse response = request.GetResponse() as HttpWebResponse;
-
- Stream responseStream = response.GetResponseStream();
-
- byte[] bArr = new byte[1024];
- int iTotalSize = 0;
- int size = responseStream.Read(bArr, 0, (int)bArr.Length);
- while (size > 0)
- {
- iTotalSize += size;
- fs.Write(bArr, 0, size);
- size = responseStream.Read(bArr, 0, (int)bArr.Length);
- }
- fs.Close();
- responseStream.Close();
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
- }
- }
- class IrisServices
- {
- public string Invoke(string data)
- {
- GlobalVariables.writeLog("入参:" + data);
-
- string url = string.Format("{0}/{1}", GlobalVariables.irisServiceIP, GlobalVariables.irisServiceURL);
-
-
- ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
-
- HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
-
-
- byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
- myRequest.Method = "POST";
- myRequest.ContentLength = buf.Length;
- myRequest.ContentType = "application/json";
-
-
-
- myRequest.Headers.Add("Authorization", GlobalVariables.irisServiceAuthorization);
- myRequest.MaximumAutomaticRedirections = 1;
- myRequest.AllowAutoRedirect = true;
-
- Stream stream = myRequest.GetRequestStream();
- stream.Write(buf, 0, buf.Length);
- stream.Close();
-
-
- HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
-
- StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
-
- string rtn = reader.ReadToEnd();
- reader.Close();
- myResponse.Close();
- GlobalVariables.writeLog("出参:" + rtn);
- return rtn;
- }
- }
- class YinHaiCom
- {
- string progID1 = "YinHai.CHS.InterfaceSCS";
- System.Type YinHaiComType;
- object YinHaiComInstance;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int Init()
- {
- YinHaiComType = System.Type.GetTypeFromProgID(progID1);
-
- if (YinHaiComType != null)
- {
- GlobalVariables.writeLog("开始COM组件Init");
-
- YinHaiComInstance = Activator.CreateInstance(YinHaiComType);
- if (YinHaiComInstance != null)
- {
- GlobalVariables.writeLog("Init实例创建成功");
- }
-
- object[] ParamArray = new object[2];
- ParamArray[0] = 0;
- ParamArray[1] = "";
- ParameterModifier[] ParamMods = new ParameterModifier[1];
- ParamMods[0] = new ParameterModifier(2);
- ParamMods[0][0] = true;
- ParamMods[0][1] = true;
- YinHaiComType.InvokeMember("yh_CHS_init",
- BindingFlags.Default | BindingFlags.InvokeMethod,
- null,
- YinHaiComInstance,
- ParamArray,
- ParamMods,
- null,
- null);
- string Msg = "加载成功:" + ParamArray[1].ToString();
- GlobalVariables.writeLog(Msg + "___" + ParamArray[0].ToString());
- return (int)ParamArray[0];
- }
- else
- {
- string Msg = "YinHaiComType加载失败!";
- GlobalVariables.writeLog(Msg);
- return 1;
- }
- }
- public void Call(string infno,string input,out string output)
- {
- if (YinHaiComType != null)
- {
-
-
- if (YinHaiComInstance != null)
- {
- GlobalVariables.writeLog("实例创建成功,准备调用Call服务");
- }
- else
- {
- output = "实例不存在!";
- GlobalVariables.writeLog("实例不存在");
- return;
- }
- GlobalVariables.writeLog("入参infno:" + infno);
- GlobalVariables.writeLog("入参input:" + input);
-
- object[] ParamArray = new object[3];
- ParamArray[0] = infno;
- ParamArray[1] = input;
- ParamArray[2] = "";
- ParameterModifier[] ParamMods = new ParameterModifier[1];
- ParamMods[0] = new ParameterModifier(3);
-
-
- ParamMods[0][2] = true;
- YinHaiComType.InvokeMember("yh_CHS_call",
- BindingFlags.Default | BindingFlags.InvokeMethod,
- null,
- YinHaiComInstance,
- ParamArray,
- ParamMods,
- null,
- null);
- output = ParamArray[2].ToString();
- }
- else
- {
- output = "COM加载失败!";
- GlobalVariables.writeLog("COM加载失败!");
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- public void Destroy(out string output)
- {
-
- if (YinHaiComType != null)
- {
-
- YinHaiComInstance = Activator.CreateInstance(YinHaiComType);
- if (YinHaiComInstance != null)
- {
- GlobalVariables.writeLog("实例创建成功,准备调用Call服务");
- }
- else
- {
- output = "实例不存在!";
- GlobalVariables.writeLog("实例不存在");
- return;
- }
-
- object[] ParamArray = new object[0];
- ParameterModifier[] ParamMods = new ParameterModifier[0];
- ParamMods[0] = new ParameterModifier(0);
- YinHaiComType.InvokeMember("yh_CHS_destroy",
- BindingFlags.Default | BindingFlags.InvokeMethod,
- null,
- YinHaiComInstance,
- ParamArray,
- ParamMods,
- null,
- null);
- output = "destroy成功!";
- }
- else
- {
- output = "COM加载失败!";
- GlobalVariables.writeLog("COM加载失败!");
- }
- }
- }
- #region
- class DllInvoke
- {
- public static object ManagedInvoke(string DllFileName, string NameSpace, string ClassName, string MethodName, object[] ObjArrayParams)
- {
-
-
-
-
- object o = new object();
- try
- {
-
- Assembly DllAssembly = Assembly.LoadFile(DllFileName);
-
- Type DllType = DllAssembly.GetType(NameSpace + "." + ClassName);
- if (DllType != null)
- {
-
- }
-
- MethodInfo MyMethod = DllType.GetMethod(MethodName);
- if (MyMethod != null)
- {
- object mObject = Activator.CreateInstance(DllType);
- try
- {
- if (ObjArrayParams == null)
- {
- o = MyMethod.Invoke(mObject, null);
-
- }
- else
- {
- o = MyMethod.Invoke(mObject, new object[] { ObjArrayParams[0].ToString() });
- }
- }
- catch (Exception e)
- {
- string inMes ="", outMes="";
- if (e.InnerException != null)
- { inMes = e.InnerException.Message; }
- outMes = e.Message;
- GlobalVariables.writeLog("inMes:" + inMes.ToString());
- GlobalVariables.writeLog("outMes:" + outMes.ToString());
- }
- }
- }
- catch (Exception mEx)
- {
- string inMes = "", outMes = "";
- if (mEx.InnerException != null)
- { inMes = mEx.InnerException.Message; }
- GlobalVariables.writeLog("inMes:" + inMes.ToString());
- o = mEx.Message;
- }
-
- return o;
- }
- }
-
- class YinHaiEncrypt
- {
-
- [DllImport("hsafsiyhsafe.dll", CharSet = CharSet.Ansi, EntryPoint = "gm_ecb_encrypt_key@16", CallingConvention = CallingConvention.StdCall)]
- private static extern int gm_ecb_encrypt_key(byte[] pub_key, byte[] plain, int plain_len, byte[] cipher);
-
- [DllImport("hsafsiyhsafe.dll", CharSet = CharSet.Ansi, EntryPoint = "gm_ecb_decrypt_key@16", CallingConvention = CallingConvention.StdCall)]
- private static extern int gm_ecb_decrypt_key(byte[] pub_key, byte[] cipher, int cipher_len, byte[] plain);
-
- [DllImport("hsafsitool.dll", EntryPoint = "gm_sign_key@24", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- private static extern int gm_sign_key(byte[] userid, byte[] prv_key, byte[] pub_key, byte[] msg, int msg_len, byte[] singrs);
-
- [DllImport("hsafsitool.dll", EntryPoint = "gm_verify_key@20", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- private static extern int gm_verify_key(byte[] userid, byte[] pub_key, byte[] msg, int msg_len, byte[] singrs);
- public const string pubKey = "725871EF03A7AA708708615A0B9400C14A9E7E4F0E5E14E911AE39CD9177C17685DED3BBDB1B242F09393BA7A0C852338A0D06D1619B79B70CF3CCD3754FF646";
- public const string prvKey = "BC5E29E032BF908DEB6EB180A0C61ED7F9AA7BF56C35B727A85CD65F79504384";
- public string Signature(string data)
- {
- string rtn = "";
- try
- {
- byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);
- byte[] prv_key = Encoding.UTF8.GetBytes(prvKey);
- byte[] userid = Encoding.UTF8.GetBytes("");
- byte[] msg = Encoding.UTF8.GetBytes(data);
- byte[] singrs = new byte[1024];
- if (gm_sign_key(userid, prv_key, pub_key, msg, msg.Length, singrs) > 0)
- {
- rtn = Encoding.UTF8.GetString(singrs).Replace("\u0000", "").Trim();
- }
- else
- {
- rtn = "";
- }
- return rtn;
- }
- catch (Exception ex)
- {
- rtn = "" + ex.Message;
- return rtn;
- }
- finally
- {
- GlobalVariables.writeLog("",data,rtn);
- }
- }
- public int VerifySignature(string plain, string signature)
- {
- byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);
- byte[] prv_key = Encoding.UTF8.GetBytes(prvKey);
- byte[] userid = Encoding.UTF8.GetBytes("");
- byte[] msg = Encoding.UTF8.GetBytes(plain);
- byte[] singrs = Encoding.UTF8.GetBytes(signature);
- int result = gm_verify_key(userid, pub_key, msg, msg.Length, singrs);
- return result;
- }
- public string Encrypt(string plain)
- {
-
- byte[] plainB = Encoding.UTF8.GetBytes(plain);
- byte[] cipher = new byte[(plainB.Length + 2048)];
- byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);
- GlobalVariables.writeLog("Encrypt明文:'" + plain + "'");
-
- if (gm_ecb_encrypt_key(pub_key, plainB, plainB.Length , cipher) > 0)
- {
- string result = Encoding.UTF8.GetString(cipher).Replace("\u0000", "").Trim();
- GlobalVariables.writeLog("Encrypt密文:" + result);
- return result;
- }
- else
- {
- return "";
- }
- }
- public string Decrypt(string cipher)
- {
- byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);
- byte[] cipherB = Encoding.UTF8.GetBytes(cipher);
- byte[] plain = new byte[(cipherB.Length)];
- if (gm_ecb_decrypt_key(pub_key, cipherB, cipher.Length, plain) > 0)
- {
- return Encoding.UTF8.GetString(plain);
- }
- else
- {
- return "";
- }
- }
- }
- #endregion
- }
|