123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- using Newtonsoft.Json.Linq;
- using System;
- using System.IO;
- using System.Net;
- using System.Reflection;
- using System.Text;
- using MedicalInsurance.Common;
- namespace ChengDuMedInsu2
- {
- class CenterServices
- {
- public string Post(string Url, string jsonParas)
- {
- GlobalVariables.writeLog("CenterServices.URL:" + Url);
- //GlobalVariables.writeLog("CenterServices.post入参:" + jsonParas);
- string strURL = Url;
- //创建一个HTTP请求
- GlobalVariables.writeLog(strURL);
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
- GlobalVariables.writeLog("111");
- //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();
- //GlobalVariables.writeLog("CenterServices.post出参:" + postContent);
- sRead.Close();
- //解析postContent,插入医保交易日志表
- //IrisServices iris = new IrisServices();
- //dynamic joIris = new JObject();
- //JObject joinParam = new JObject();
- //joinParam = JObject.Parse(jsonParas);
- //JObject jooutParam = new JObject();
- //jooutParam = JObject.Parse(postContent);
- //joIris.Add("inParam", JObject.FromObject(joinParam));
- //joIris.Add("outParam", JObject.FromObject(jooutParam));
- //joIris.code = "09010021";
- //joIris.HospitalID = GlobalVariables.hospitalID;
- //joIris.InterfaceID = GlobalVariables.InterfaceID;
- //iris.Invoke(joIris.ToString());
-
- return postContent;//返回Json数据
- }
- public string Download(string url, string strParams)
- {
- string error = string.Empty; int errorCode = 0;
- try
- {
-
- GlobalVariables.writeLog("CenterServices.Download入参:" + strParams);
- JObject jsonInParam = JObject.Parse(strParams);
- string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];
- string filePath = System.Environment.CurrentDirectory +"\\Download\\" + fileName;
- //如果不存在目录,则创建目录
- if (!Directory.Exists(System.Environment.CurrentDirectory + "\\Download"))
- {
- //创建文件夹
- DirectoryInfo dirInfo = Directory.CreateDirectory(System.Environment.CurrentDirectory + "\\Download");
- }
- 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;
- errorCode = -100;
- error ="连接服务器失败!";
- }
- //将请求参数写入流
- 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();
- dynamic joReturn = new JObject();
- joReturn.errorCode = errorCode;
- joReturn.error = error;
- joReturn.filePath = filePath;
- GlobalVariables.writeLog("CenterServices.Download出参:" + joReturn.ToString());
- return joReturn.ToString();
- }
- catch (Exception ex)
- {
- errorCode = -100;
- error = ex.Message;
- dynamic joReturn = new JObject();
- joReturn.errorCode = errorCode;
- joReturn.error = error;
- GlobalVariables.writeLog("CenterServices.Download出参:" + joReturn.ToString());
- return joReturn.ToString();
- }
- }
- }
- class IrisServices
- {
- public string Invoke(string data)
- {
- //GlobalVariables.writeLog("IrisServices.Invoke入参:" + 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("IrisServices.Invoke出参:" + 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)
- {
- try
- {
- 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();
- GlobalVariables.writeLog("Com输出:" + output);
- }
- else
- {
- output = "COM加载失败!";
- GlobalVariables.writeLog("COM加载失败!");
- }
- }
- catch (Exception ex)
- {
- output = ex.Message;
- GlobalVariables.writeLog("COM加载失败!"+ output);
- }
- // 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 Print(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[2];
- ParamArray[0] = input;
- ParamArray[1] = "";
- ParameterModifier[] ParamMods = new ParameterModifier[1];
- ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数
- //ParamMods[0][0] = false;
- //ParamMods[0][1] = false;
- ParamMods[0][1] = true;
- YinHaiComType.InvokeMember("yh_CHS_print", // 接口函数名
- BindingFlags.Default | BindingFlags.InvokeMethod,
- null,
- YinHaiComInstance, // 调用的COM组件
- ParamArray, // 参数数组
- ParamMods, // 指定返回参数的ParameterModifier数组
- null,
- null);
- output = ParamArray[1].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加载失败!");
- }
- }
- }
- }
|