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;
- //创建一个HTTP请求
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
- //Post请求方式
- request.Method = "POST";
- //内容类型
- request.ContentType = "application/json";
- //设置参数,并进行URL编码
- string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas);
- byte[] payload;
- //将Json字符串转化为字节
- payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
- //设置请求的ContentLength
- request.ContentLength = payload.Length;
- //发送请求,获得请求流
- Stream writer;
- try
- {
- writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
- }
- catch (Exception)
- {
- writer = null;
- GlobalVariables.writeLog("连接服务器失败!");
- }
- //将请求参数写入流
- writer.Write(payload, 0, payload.Length);
- writer.Close();//关闭请求流
- // String strValue = "";//strValue为http响应所返回的字符流
- HttpWebResponse response;
- try
- {
- //获得响应流
- response = (HttpWebResponse)request.GetResponse();
- }
- catch (WebException ex)
- {
- response = ex.Response as HttpWebResponse;
- }
- Stream s = response.GetResponseStream();
- // Stream postData = Request.InputStream;
- StreamReader sRead = new StreamReader(s);
- string postContent = sRead.ReadToEnd();
- sRead.Close();
- //解析postContent,插入医保交易日志表
- 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;//返回Json数据
- }
- 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;
- //创建一个HTTP请求
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
- //Post请求方式
- request.Method = "POST";
- //内容类型
- request.ContentType = "application/json";
- //设置参数,并进行URL编码
- string paraUrlCoded = strParams;//System.Web.HttpUtility.UrlEncode(jsonParas);
- byte[] payload;
- //将Json字符串转化为字节
- payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
- //设置请求的ContentLength
- request.ContentLength = payload.Length;
- Stream writer;
- try
- {
- writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
- }
- catch (Exception)
- {
- writer = null;
- Console.Write("连接服务器失败!");
- }
- //将请求参数写入流
- writer.Write(payload, 0, payload.Length);
- writer.Close();//关闭请求流
- // String strValue = "";//strValue为http响应所返回的字符流
- //发送请求并获取相应回应数据
- HttpWebResponse response = request.GetResponse() as HttpWebResponse;
- //直到request.GetResponse()程序才开始向目标网页发送Post请求
- 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);
- //先根据用户请求的uri构造请求地址
- string url = string.Format("{0}/{1}", GlobalVariables.irisServiceIP, GlobalVariables.irisServiceURL);
- //log.Write(serviceUrl);
- //log.Write(data);
- ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
- //创建Web访问对象
- HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
- //
- //把用户传过来的数据转成“UTF-8”的字节流
- 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", hisauthorization);
- //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");
- //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");
- myRequest.Headers.Add("Authorization", GlobalVariables.irisServiceAuthorization);
- myRequest.MaximumAutomaticRedirections = 1;
- myRequest.AllowAutoRedirect = true;
- //发送请求
- Stream stream = myRequest.GetRequestStream();
- stream.Write(buf, 0, buf.Length);
- stream.Close();
- //获取接口返回值
- //通过Web访问对象获取响应内容
- HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
- //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
- StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
- //string rtn = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
- string rtn = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
- reader.Close();
- myResponse.Close();
- GlobalVariables.writeLog("出参:" + rtn);
- return rtn;
- }
- }
- class YinHaiCom
- {
- string progID1 = "YinHai.CHS.InterfaceSCS";
- System.Type YinHaiComType;
- object YinHaiComInstance;
- //public YinHaiCom()
- //{
- // // 与指定 ProgID 关联的类型,即获取相应的Com对象
- // if (YinHaiComType == null)
- // {
- // GlobalVariables.writeLog("开始创建银海Com对象");
- // YinHaiComType = System.Type.GetTypeFromProgID(progID);
- // Init();
-
- // }
- // else
- // {
- // GlobalVariables.writeLog("银海Com对象已存在");
- // }
- //}
- public int Init()
- {
- YinHaiComType = System.Type.GetTypeFromProgID(progID1);
- // 创建Com的实例
- 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; // 设置第二个参数为返回参数,调用含有ParameterModifier数组的重载函数
- YinHaiComType.InvokeMember("yh_CHS_init", // 接口函数名
- BindingFlags.Default | BindingFlags.InvokeMethod,
- null,
- YinHaiComInstance, // 调用的COM组件
- ParamArray, // 参数数组
- ParamMods, // 指定返回参数的ParameterModifier数组
- 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)
- {
- //创建实例,不能再次创建,否则会提示没有初始化
- //YinHaiComInstance = Activator.CreateInstance(YinHaiComType);
- 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][0] = false;
- //ParamMods[0][1] = false;
- ParamMods[0][2] = true;
- YinHaiComType.InvokeMember("yh_CHS_call", // 接口函数名
- BindingFlags.Default | BindingFlags.InvokeMethod,
- null,
- YinHaiComInstance, // 调用的COM组件
- ParamArray, // 参数数组
- ParamMods, // 指定返回参数的ParameterModifier数组
- null,
- null);
- output = ParamArray[2].ToString();
- }
- else
- {
- output = "COM加载失败!";
- GlobalVariables.writeLog("COM加载失败!");
- }
- // if (YinHaiComType == null)
- // {
- // output = "未找到组件,请先调用ComInit或者检查运行环境!";
- // GlobalVariables.writeLog(output);
- // return ;
- // }
- // //设置需要设置的参数值
- // 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; // 设置第二个参数为返回参数
- // //调用含有ParameterModifier数组的重载函数
- // YinHaiComType.InvokeMember("yh_CHS_call", // 接口函数名
- // BindingFlags.Default | BindingFlags.InvokeMethod,
- // null,
- // YinHaiComInstance, // 调用的COM组件
- // ParamArray, // 参数数组
- // ParamMods, // 指定返回参数的ParameterModifier数组
- // null,
- //null);
- // output = ParamArray[2].ToString();
- }
- public void Destroy(out string output)
- {
- // 创建Com的实例
- 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, // 调用的COM组件
- ParamArray, // 参数数组
- ParamMods, // 指定返回参数的ParameterModifier数组
- 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)
- {
- //GlobalVariables.writeLog("程序集信息:" + NameSpace + "." + ClassName + "." + MethodName);
- //GlobalVariables.writeLog("文件全路径:" + DllFileName);
- //GlobalVariables.writeLog("命名空间.类.方法:" + NameSpace + "." + ClassName + "." + MethodName);
- //GlobalVariables.writeLog("入参:" + ObjArrayParams[0].ToString());
- object o = new object();
- try
- {
- //载入程序集
- Assembly DllAssembly = Assembly.LoadFile(DllFileName);
- //MessageBox.Show(NameSpace + ":" + ClassName);
- Type DllType = DllAssembly.GetType(NameSpace + "." + ClassName);
- if (DllType != null)
- {
- //MessageBox.Show("32");
- }
- //查找要调用的方 法并进行调用
- 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;
- }
- //GlobalVariables.writeLog("出参:" + o.ToString());
- 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
- }
|