| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354 | /****************************************************************************** * 文件名称: InvokeHelper.cs * 文件说明: 调用助手,调用方法的封装 * 当前版本: V1.0 * 创建日期: 2022-04-12 * * 2020-04-12: 增加 businessDLLInvoke 方法 * 2020-04-12: 增加 writeLog 方法 * 2020-04-14: 增加 businessDLLInvoke(重载) 方法 * 2020-04-14: 增加 irisServiceInvoke 方法******************************************************************************/using Newtonsoft.Json.Linq;using Newtonsoft.Json;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using PTMedicalInsurance.Helper;using PTMedicalInsurance.Common;using PTMedicalInsurance.Variables;using System.Runtime.InteropServices;using System.Reflection;using PTMedicalInsurance.Helper;using PTMedicalInsurance.APIGATEWAY_SDK;using PTMedicalInsurance.Forms;namespace PTMedicalInsurance.Helper{        class InvokeHelper    {        private string serviceURL;        private string authorization;        string progID = "yinhai.yh_hb_sctr";        System.Type YinHaiComType;        object YinHaiComInstance;             public int yh_hb_call(string infno, string input, out string output)        {            try            {                Type YinHaiComType = System.Type.GetTypeFromProgID("yinhai.yh_hb_sctr");                if (YinHaiComType != null)                {                    //创建实例,不能再次创建,否则会提示没有初始化                                        object YinHaiComInstance = System.Activator.CreateInstance(YinHaiComType);                    try                    {                        if (YinHaiComInstance != null)                        {                            Global.writeLog("实例创建成功,准备调用yh_hb_call服务");                        }                        else                        {                            output = "实例不存在!";                            Global.writeLog("实例不存在");                            return -1;                        }                        //GlobalVariables.writeLog("入参infno:" + infno);                        //GlobalVariables.writeLog("入参input:" + input);                        //设置需要设置的参数值                        Global.writeLog("开始调用入参" + 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_hb_call", // 接口函数名                         BindingFlags.Default | BindingFlags.InvokeMethod,                        null,                        YinHaiComInstance, // 调用的COM组件                        ParamArray, // 参数数组                        ParamMods, // 指定返回参数的ParameterModifier数组                        null,                        null);                        output = ParamArray[2].ToString();                        Global.writeLog("Com输出:" + output);                        //Marshal.ReleaseComObject(YinHaiComInstance);//释放comm组件                        return 0;                    }                    finally                    {                        if (YinHaiComInstance != null)                        {                            Marshal.ReleaseComObject(YinHaiComInstance);                        }                        GC.Collect();                                                                     }                }                else                {                    output = "COM加载失败!";                    Global.writeLog("COM加载失败!");                    return -1;                }            }            catch (Exception ex)            {                output = ex.Message;                Global.writeLog("COM加载失败!" + output);                System.Diagnostics.EventLog.WriteEntry("Application", "Error occurred: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);                return -1;            }        }                      /// <summary>        /// iris服务调用的封装        /// </summary>        /// <param name="data"></param>        /// <returns></returns>        public JObject invokeIrisService(string data, string serviceDesc)        {            string rtn = "", url = "";            JObject joRtn = new JObject();            try            {                //先根据用户请求的uri构造请求地址                url = serviceURL;                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", authorization);                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());//如果有编码问题就用这个方法                rtn = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾                reader.Close();                myResponse.Close();                joRtn = JObject.Parse(rtn);                return joRtn;            }            catch (Exception ex)            {                joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);                rtn = JsonConvert.SerializeObject(joRtn);                return joRtn;            }        }        /// <summary>        /// HIS服务调用的封装        /// </summary>        /// <param name="data"></param>        /// <returns></returns>        public JObject invokeHISService(string data, string serviceDesc)        {            JObject joRtn = new JObject();            try            {                //先根据用户请求的uri构造请求地址                serviceURL = string.Format("{0}/{1}", Global.hisConfig.ip, Global.hisConfig.url);                authorization = Global.hisConfig.authorization;                joRtn = invokeIrisService(data, serviceDesc);                return joRtn;            }            catch (Exception ex)            {                joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);                return joRtn;            }            finally            {                Global.writeLog_Iris(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), JsonHelper.Compress(joRtn));            }        }        /// <summary>        /// 医保平台服务调用的封装        /// </summary>        /// <param name="data"></param>        /// <returns></returns>        public JObject invokeInsuService(string data, string serviceDesc)        {            string rtn = "";            JObject joRtn = new JObject();            try            {                //先根据用户请求的uri构造请求地址                serviceURL = string.Format("{0}/{1}", Global.insuConfig.ip, Global.insuConfig.url);                authorization = Global.insuConfig.authorization;                joRtn = invokeIrisService(data, serviceDesc);                rtn = JsonConvert.SerializeObject(joRtn);                //if (serviceDesc == "插入签到信息")                //{                //    MessageBox.Show("插入签到信息入参:" + data +"|返回值:"+ rtn.ToString()+"|"+ Global.insuConfig.url);                //}                return joRtn;            }            catch (Exception ex)            {                joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);                rtn = JsonConvert.SerializeObject(joRtn);                return joRtn;            }            finally            {                Global.writeLog_Iris(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), rtn);            }        }        public JObject invokeCenterServicePresCir(string funNO, string data)        {            JObject joRtn = new JObject();            string outPar = "";            try            {                if (funNO == "7101")                {                    Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/uploadChk";                }                else if (funNO == "7102")                {                    Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxFixmedinsSign";                }                else if (funNO == "7103")                {                    Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxFileUpld";                }                else if (funNO == "7104")                {                    Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxUndo";                }                else if (funNO == "7105")                {                    Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/hospRxDetlQuery";                }                else if (funNO == "7106")                {                    Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxChkInfoQuery";                }                else if (funNO == "7107")                {                    Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxSetlInfoQuery";                }                else if (funNO == "7108")                {                    Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxChkInfoCallback";                }                else if (funNO == "7109")                {                    Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxSetlInfoCallback";                }                else                {                    Global.curEvt.URL = Global.inf.centerURL;                }                //Global.curEvt.URL = Global.inf.centerURL;                joRtn = invokeCenterServicePresCir(data);                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            catch (Exception ex)            {                if (joRtn["infcode"] == null)                { joRtn.Add("infcode", -1); }                if (joRtn["err_msg"] == null)                { joRtn.Add("err_msg", "invokeCenterServicePresCir(3):" + ex.Message); }                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            finally            {                Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), joRtn.ToString());                //this.saveCenterLog(JsonHelper.Compress(data), outPar, JsonHelper.Compress(data), outPar);            }        }        /// <summary>        /// 医保电子处方流转调用服务        /// </summary>        /// <param name="data"></param>        /// <returns></returns>        private JObject invokeCenterServicePresCir(string data)        {            string postContent = "";            JObject joRtn = new JObject();            try            {                //内容类型                                Signer signer = new Signer();                signer.Key = Global.inf.privateKey;                     //应用编码                signer.Secret = Global.inf.Secret;               //secretKey 私钥                string timestamp = TimeStamp.get13().ToString();  //当前时间戳(秒)                string nonce = Guid.NewGuid().ToString();         //非重复的随机字符串(十分钟内不能重复)                HttpRequest Resquest = new HttpRequest("POST", new Uri(Global.curEvt.URL));                Resquest.headers.Add("charset", "UTF-8");                //Resquest.headers.Add("x-hw-id", signer.Key);                //Resquest.headers.Add("x-tif-timestamp", timestamp);                //Resquest.headers.Add("x-tif-passid", signer.Key);                //Resquest.headers.Add("x-tif-nonce", nonce);                Resquest.body = data;                string RtnStr;                HttpWebRequest req = signer.Sign(Resquest);                req.ContentType = "application/json;charset=utf8";                req.Timeout = 5 * 10000;                try                {                    var writer = new StreamWriter(req.GetRequestStream());                    writer.Write(Resquest.body);                    writer.Flush();                    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();                    StreamReader reader = new StreamReader(resp.GetResponseStream());                    RtnStr = reader.ReadToEnd();                    joRtn = JObject.Parse(RtnStr);//返回Json数据                    return joRtn;                }                catch (WebException e)                {                    HttpWebResponse resp = (HttpWebResponse)e.Response;                    if (resp != null)                    {                        return JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常(a)", new StreamReader(resp.GetResponseStream()).ReadToEnd() + "异常内容:" + e.Message);                    }                    else                    {                        RtnStr = "异常:" + e.Message;                        return JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常(b)", e.Message);                    }                }            }            catch (Exception ex)            {                postContent = "调用中心服务异常" + ex.Message;                joRtn.Add("infcode", -1);                joRtn.Add("err_msg", "invokeCenterService(Exception_Last):" + ex.Message);                return joRtn;            }        }        #region 【移动支付】        public JObject invokeMPServiceCom(string funNO, string data)        {            return invokeMPServiceCom(funNO, JObject.Parse(data));        }        public JObject invokeMPServiceCom(string funNO, JObject joInput)        {            JObject joRtn = new JObject();            String outPar = "";            try            {                string url = "";                switch (funNO)                {                    case "6201":                        url = "/org/local/api/hos/uldFeeInfo";                        break;                    case "6202":                        url = "/org/local/api/hos/pay_order";                        break;                    case "6203":                        url = "/org/local/api/hos/refund_Order";                        break;                    case "6301":                        url = "/org/local/api/hos/query_order_info";                        break;                    case "6401":                        url = "/org/local/api/hos/revoke_order";                        break;                    default:                        break;                }                EncryptHelper encrypt = new EncryptHelper();                if (funNO == "6201")                    Global.inf.mobilePayURL = @"http://10.87.67.100:8080";                else                    Global.inf.mobilePayURL = @"http://10.87.67.119:8080";                string data = JsonHelper.setMPCenterInpar(funNO, joInput);                // 移动支付地址                Global.curEvt.URL = Global.inf.mobilePayURL + url;                Global.inf.centerURL = Global.curEvt.URL;                string outputData = "", errMsg = "";                // 动态调试模式                if (Global.curEvt.enabledDebug)                {                    CenterResult center = new CenterResult();                    center.setTradeNo(funNO);                    if (center.ShowDialog() == DialogResult.OK)                    {                        outPar = center.returnData;                        return JObject.Parse(outPar);                    }                }                try                {                    InvokeRestCenter mobileCenter = new InvokeRestCenter();                    //int iInt = mobileCenter.BusinessCom(funNO, data, ref outputData, ref errMsg);                    int iInt = mobileCenter.Business(data, ref outputData, ref errMsg);                    joRtn = JObject.Parse(outputData);                    string encData = JsonHelper.getDestValue(joRtn, "encData");                    string signData = JsonHelper.getDestValue(joRtn, "signData");                    if (!string.IsNullOrEmpty(encData) && !string.IsNullOrEmpty(signData))                    {                        joRtn.Remove("encData");                        joRtn.Remove("signData");                        joRtn.Remove("data");                        //解密                        string decData = encrypt.decrypt00(encData);                        // 验签                        JsonConvert.DefaultSettings = () => new JsonSerializerSettings                        {                            FloatParseHandling = FloatParseHandling.Decimal                        };                        joRtn.Add("data", JToken.FromObject(JsonConvert.DeserializeObject(decData)));                        bool rtn = encrypt.verify(joRtn, signData);                        if (rtn)                        {                            joRtn = JObject.Parse(decData);                            joRtn.Add("success", "True");                        }                        else                        {                            Global.writeLog("验签失败,请核查!");                        }                    }                    return joRtn;                }                finally                {                    this.saveCenterLog(JsonHelper.Compress(data), joRtn.ToString(), JsonHelper.Compress(data), joRtn.ToString());                }            }            catch (Exception ex)            {                if (joRtn["infcode"] == null)                { joRtn.Add("infcode", -1); }                if (joRtn["err_msg"] == null)                { joRtn.Add("err_msg", "invokeCenterService(3):" + ex.Message); }                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            finally            {                Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", joInput.ToString(), joRtn.ToString());                this.saveCenterLog(joInput.ToString(), joRtn.ToString(), joInput.ToString(), joRtn.ToString());            }        }        /// <summary>        /// 移动        /// </summary>        /// <param name="funNO"></param>        /// <param name="data"></param>        /// <returns></returns>        public JObject invokeMPService(string funNO, string data)        {            return invokeMPService(funNO, JObject.Parse(data));        }        public JObject invokeMPService(string funNO, JObject joInput)        {            JObject joRtn = new JObject();            String outPar = "";            try            {                string url = "";                switch (funNO)                {                    case "6201":                        url = "/org/local/api/hos/uldFeeInfo";                        break;                    case "6202":                        url = "/org/local/api/hos/pay_order";                        break;                    case "6203":                        url = "/org/local/api/hos/refund_Order";                        break;                    case "6301":                        url = "/org/local/api/hos/query_order_info";                        break;                    case "6401":                        url = "/org/local/api/hos/revoke_order";                        break;                    default:                        break;                }                EncryptHelper encrypt = new EncryptHelper();                //测试地址                //if (funNO == "6201")                //    Global.inf.mobilePayURL = @"http:\\10.87.67.100:8080";                //else                //    Global.inf.mobilePayURL = @"http:\\10.87.67.119:8080";                if (funNO == "6201")                    Global.inf.mobilePayURL = @"http:\\10.87.82.238:8080";                else                    Global.inf.mobilePayURL = @"http:\\10.87.80.251:8080";                string data = JsonHelper.setMPCenterInpar(funNO, joInput);                // 移动支付地址                Global.curEvt.URL = Global.inf.mobilePayURL + url;                Global.inf.centerURL = Global.curEvt.URL;                string outputData = "", errMsg = "";                // 动态调试模式                if (Global.curEvt.enabledDebug)                {                    CenterResult center = new CenterResult();                    center.setTradeNo(funNO);                    if (center.ShowDialog() == DialogResult.OK)                    {                        outPar = center.returnData;                        return JObject.Parse(outPar);                    }                }                try                {                    InvokeRestCenter mobileCenter = new InvokeRestCenter();                    int iInt = mobileCenter.Business(data, ref outputData, ref errMsg);                    joRtn = JObject.Parse(outputData);                    string encData = JsonHelper.getDestValue(joRtn, "encData");                    string signData = JsonHelper.getDestValue(joRtn, "signData");                    if (!string.IsNullOrEmpty(encData) && !string.IsNullOrEmpty(signData))                    {                        joRtn.Remove("encData");                        joRtn.Remove("signData");                        joRtn.Remove("data");                        //解密                        string decData = encrypt.decrypt00(encData);                        // 验签                        JsonConvert.DefaultSettings = () => new JsonSerializerSettings                        {                            FloatParseHandling = FloatParseHandling.Decimal                        };                        joRtn.Add("data", JToken.FromObject(JsonConvert.DeserializeObject(decData)));                        bool rtn = encrypt.verify(joRtn, signData);                        if (rtn)                        {                            joRtn = JObject.Parse(decData);                            joRtn.Add("success", "True");                        }                        else                        {                            Global.writeLog("验签失败,请核查!");                        }                    }                    return joRtn;                }                finally                {                    this.saveCenterLog(JsonHelper.Compress(data), joRtn.ToString(), JsonHelper.Compress(data), joRtn.ToString());                }            }            catch (Exception ex)            {                if (joRtn["infcode"] == null)                { joRtn.Add("infcode", -1); }                if (joRtn["err_msg"] == null)                { joRtn.Add("err_msg", "invokeCenterService(3):" + ex.Message); }                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            finally            {                Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", joInput.ToString(), joRtn.ToString());                this.saveCenterLog(joInput.ToString(), joRtn.ToString(), joInput.ToString(), joRtn.ToString());            }        }        #endregion        /// <summary>        /// 医保中心Post服务调用封装        /// </summary>        /// <param name="data"></param>        /// <returns></returns>        private JObject invokeCenterService(string data)        {            string postContent = "";            JObject joRtn = new JObject();            try            {                //创建一个HTTP请求                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);                //Post请求方式                  request.Method = "POST";                //内容类型                request.ContentType = "application/json";                request.Headers.Add("apikey", "fa8kQMIF6LB0D7c6UGQoZc6TCcZO2G6O");                request.Accept = "text/html, application/xhtml+xml, */*";                    //昆明增加头部信息                /*                string nonce = Guid.NewGuid().ToString();         //非重复的随机字符串(十分钟内不能重复)                string timestamp = TimeStamp.get13().ToString();  //当前时间戳(秒)                string BusinessID = Global.inf.BusinessID;        //服务商ID                string InsuHosID = Global.inf.hospitalNO;         //医疗机构ID                string CreditID = Global.inf.CreditID;            //服务商统一社会信用代码                    string sTemp = timestamp + BusinessID + nonce;                //Sha256 加密生成的签名 signature = sha256(hsf_timestamp + infosyssign + hsf_nonce)                string signature = Encrypt.SHA256EncryptStr(sTemp);                request.Headers.Add("hsf_signature", signature);                request.Headers.Add("hsf_timestamp", timestamp);                request.Headers.Add("hsf_nonce", nonce);                request.Headers.Add("fixmedins_code", InsuHosID);                request.Headers.Add("infosyscode", CreditID);                */                //设置参数,并进行URL编码                 string paraUrlCoded = data;//System.Web.HttpUtility.UrlEncode(jsonParas);                   byte[] payload;                //将Json字符串转化为字节                  payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);                 //设置请求的ContentLength                   request.ContentLength = payload.Length;                //发送请求,获得请求流                 Stream writer;                writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象                                                    //将请求参数写入流                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;                    return JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常", ex.Message);                }                Stream s = response.GetResponseStream();                StreamReader sRead = new StreamReader(s);                postContent = sRead.ReadToEnd();                sRead.Close();                joRtn = JObject.Parse(postContent);//返回Json数据                return joRtn;            }            catch (Exception ex)            {                postContent = "调用中心服务异常" + ex.Message;                joRtn.Add("infcode", -1);                joRtn.Add("err_msg", "invokeCenterService(1):" + ex.Message);                return joRtn;            }        }              /// <summary>        /// 这个是调用业务服务的invokeCenterService        /// </summary>        /// <param name="funNO"></param>        /// <param name="data"></param>        /// <returns></returns>        public JObject invokeCenterServiceApi(string funNO, JObject data)        {            JObject joRtn = new JObject();            string outPar = ""; Boolean bDownLoad = false;            int iInt = 0;            try            {                Global.curEvt.URL = Global.inf.centerURL;                joRtn = invokeCenterService(data.ToString());                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            catch (Exception ex)            {                if (joRtn["infcode"] == null)                { joRtn.Add("infcode", -1); }                if (joRtn["err_msg"] == null)                { joRtn.Add("err_msg", "invokeCenterService(2):" + ex.Message); }                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            finally            {                Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), joRtn.ToString());                this.saveCenterLog(JsonHelper.Compress(data), outPar, JsonHelper.Compress(data), outPar);            }          }        /// <summary>        /// 这个是调用业务服务的invokeCenterService        /// 湖北进销存接口调用        /// </summary>        /// <param name="funNO"></param>        /// <param name="data"></param>        /// <returns></returns>        public JObject invokeCenterServiceJXC(string funNO, JObject data)        {            JObject joRtn = new JObject();            string outPar = "";            try            {                // 动态调试模式                if (Global.curEvt.enabledDebug)                {                    CenterResult center = new CenterResult();                    center.setTradeNo(funNO);                    if (center.ShowDialog() == DialogResult.OK)                    {                        outPar = center.returnData;                        return JObject.Parse(outPar);                    }                }                //setCenterURL(funNO);                Global.curEvt.URL = "http://prod.xxhgl.whsybj.cn/mrss-data/api/uts/callService";//市医保进销存地址                   if (Global.inf.interfaceDr == 43) //湖北普瑞测试环境                {                    Global.curEvt.URL = "http://test.xxhgl.whsybj.cn/mrss-data/api/uts/callService";                }                if (Global.inf.interfaceDr == 45) //湖北普瑞测试环境                {                    Global.curEvt.URL = "http://test.xxhgl.whsybj.cn/mrss-data/api/uts/callService";                }                joRtn = invokeCenterServiceJXC(JsonHelper.setCenterInparInventory(data));//调用前加密                Global.writeLog("返回解密后"+ joRtn.ToString());                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            catch (Exception ex)            {                if (joRtn["infcode"] == null)                { joRtn.Add("infcode", joRtn["code"]); }                if (joRtn["err_msg"] == null)                { //joRtn.Add("err_msg", "invokeCenterService(2):" + ex.Message);                    joRtn.Add("err_msg", joRtn["message"]);                                    }                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            finally            {                Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), joRtn.ToString());                this.saveCenterLog(JsonHelper.Compress(data), joRtn.ToString(), JsonHelper.Compress(data), joRtn.ToString());            }        }        /// <summary>        /// 市医保医保进销存Post服务调用封装        /// </summary>        /// <param name="data"></param>        /// <returns></returns>        private JObject invokeCenterServiceJXC(string data)        {            string postContent = "";            JObject joRtn = new JObject();            try            {                Global.writeLog(string.Format("调用中心{0}接口入参:{1}", data, Global.curEvt.URL));                //创建一个HTTP请求                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);                //Post请求方式                  request.Method = "POST";                //string nonce = Guid.NewGuid().ToString();         //非重复的随机字符串(十分钟内不能重复)                //string timestamp = TimeStamp.get13().ToString();  //当前时间戳(秒)                //string BusinessID = Global.inf.BusinessID;        //服务商ID                //string InsuHosID = Global.inf.hospitalNO;         //医疗机构ID                //string CreditID = Global.inf.CreditID;            //服务商统一社会信用代码                                //内容类型                request.ContentType = "application/json;charset=UTF-8";                //昆明增加头部信息                //string sTemp = timestamp + BusinessID + nonce;                //Sha256 加密生成的签名 signature = sha256(hsf_timestamp + infosyssign + hsf_nonce)                //string signature = Encrypt.SHA256EncryptStr(sTemp);                //request.Headers.Add("hsf_signature", signature);                //request.Headers.Add("hsf_timestamp", timestamp);                //request.Headers.Add("hsf_nonce", nonce);                //request.Headers.Add("fixmedins_code", InsuHosID);                //request.Headers.Add("infosyscode", CreditID);                //设置参数,并进行URL编码                 string paraUrlCoded = data;//System.Web.HttpUtility.UrlEncode(jsonParas);                   byte[] payload;                //将Json字符串转化为字节                  payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);                //设置请求的ContentLength                   request.ContentLength = payload.Length;                //发送请求,获得请求流                 Stream writer;                writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象                                                    //将请求参数写入流                writer.Write(payload, 0, payload.Length);                writer.Close();//关闭请求流                               // String strValue = "";//strValue为http响应所返回的字符流                HttpWebResponse response = null;                try                {                    //获得响应流                    response = (HttpWebResponse)request.GetResponse();                }                catch (WebException ex)                {                    // return JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常", ex.Message);                    HttpWebResponse res = (HttpWebResponse)ex.Response;                    Stream myResponseStream = res.GetResponseStream();                    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);                    string retString = myStreamReader.ReadToEnd();                    return JsonHelper.setExceptionJson(-99, "获得响应流异常", retString);                }                postContent = getResponseData(response);                joRtn = JObject.Parse(postContent);//返回Json数据                if (joRtn.ContainsKey("body"))                {                    joRtn = (JObject)joRtn.GetValue("body");                }                Global.writeLog("返回解密前" + joRtn.ToString());                return JObject.Parse(JsonHelper.insDecryptInventory(joRtn));//返回前解密            }            catch (Exception ex)            {                postContent = "调用中心服务异常" + ex.Message;                joRtn.Add("infcode", -1);                joRtn.Add("err_msg", "invokeCenterService(1):" + ex.Message);                return joRtn;                            }        }        /// <summary>        /// 这个是调用业务服务的invokeCenterService        /// 湖北进销存辅助接口I2001-I2002接口调用        /// </summary>        /// <param name="funNO"></param>        /// <param name="data"></param>        /// <returns></returns>        public JObject invokeCenterServiceFZJK(string funNO, JObject data)        {            JObject joRtn = new JObject();            string outPar = "";            try            {                // 动态调试模式                if (Global.curEvt.enabledDebug)                {                    CenterResult center = new CenterResult();                    center.setTradeNo(funNO);                    if (center.ShowDialog() == DialogResult.OK)                    {                        outPar = center.returnData;                        return JObject.Parse(outPar);                    }                }                //setCenterURL(funNO);                //湖北进销存辅助接口                //12001 : http://ymtng.whsybj.cn/api/wuhan/queryMedicallnfo                //12002: http://ymtng.whsybj.cn/api/wuhan/queryCodeRelation                if(funNO== "I2001")                {                    Global.curEvt.URL = "http://ymtng.whsybj.cn/api/wuhan/queryMedicallnfoe";//市医保进销存地址                }                else if (funNO == "I2002")                {                    Global.curEvt.URL = "http://ymtng.whsybj.cn/api/wuhan/queryCodeRelation";//市医保进销存地址                }                //joRtn = invokeCenterServiceFZJK(JsonHelper.setCenterInparInventory(data));//调用前加密                joRtn = invokeCenterServiceFZJK(data.ToString());//调用前加密                Global.writeLog("返回解密后" + joRtn.ToString());                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            catch (Exception ex)            {                if (joRtn["infcode"] == null)                { joRtn.Add("infcode", joRtn["code"]); }                if (joRtn["err_msg"] == null)                { //joRtn.Add("err_msg", "invokeCenterService(2):" + ex.Message);                    joRtn.Add("err_msg", joRtn["message"]);                }                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            finally            {                Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), joRtn.ToString());                this.saveCenterLog(JsonHelper.Compress(data), joRtn.ToString(), JsonHelper.Compress(data), joRtn.ToString());            }        }        /// <summary>        /// 市医保医保进销存辅助接口I2001-I2002-Post服务调用封装        /// </summary>        /// <param name="data"></param>        /// <returns></returns>        private JObject invokeCenterServiceFZJK(string data)        {            string postContent = "";            JObject joRtn = new JObject();            try            {                Global.writeLog(string.Format("调用中心{0}接口入参:{1}", data, Global.curEvt.URL));                //创建一个HTTP请求                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);                //Post请求方式                  request.Method = "POST";                //string nonce = Guid.NewGuid().ToString();         //非重复的随机字符串(十分钟内不能重复)                //string timestamp = TimeStamp.get13().ToString();  //当前时间戳(秒)                //string BusinessID = Global.inf.BusinessID;        //服务商ID                //string InsuHosID = Global.inf.hospitalNO;         //医疗机构ID                //string CreditID = Global.inf.CreditID;            //服务商统一社会信用代码                                //内容类型                request.ContentType = "application/json;charset=UTF-8";                //昆明增加头部信息                //string sTemp = timestamp + BusinessID + nonce;                //Sha256 加密生成的签名 signature = sha256(hsf_timestamp + infosyssign + hsf_nonce)                //string signature = Encrypt.SHA256EncryptStr(sTemp);                //request.Headers.Add("hsf_signature", signature);                //request.Headers.Add("hsf_timestamp", timestamp);                //request.Headers.Add("hsf_nonce", nonce);                //request.Headers.Add("fixmedins_code", InsuHosID);                //request.Headers.Add("infosyscode", CreditID);                //设置参数,并进行URL编码                 string paraUrlCoded = data;//System.Web.HttpUtility.UrlEncode(jsonParas);                   byte[] payload;                //将Json字符串转化为字节                  payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);                //设置请求的ContentLength                   request.ContentLength = payload.Length;                //发送请求,获得请求流                 Stream writer;                writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象                                                    //将请求参数写入流                writer.Write(payload, 0, payload.Length);                writer.Close();//关闭请求流                               // String strValue = "";//strValue为http响应所返回的字符流                HttpWebResponse response = null;                try                {                    //获得响应流                    response = (HttpWebResponse)request.GetResponse();                }                catch (WebException ex)                {                    // return JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常", ex.Message);                    HttpWebResponse res = (HttpWebResponse)ex.Response;                    Stream myResponseStream = res.GetResponseStream();                    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);                    string retString = myStreamReader.ReadToEnd();                    return JsonHelper.setExceptionJson(-99, "获得响应流异常", retString);                }                postContent = getResponseData(response);                joRtn = JObject.Parse(postContent);//返回Json数据                if (joRtn.ContainsKey("body"))                {                    joRtn = (JObject)joRtn.GetValue("body");                }                //return JObject.Parse(JsonHelper.insDecryptInventory(joRtn));//返回前解密                return joRtn;//返回前解密            }            catch (Exception ex)            {                postContent = "调用中心服务异常" + ex.Message;                joRtn.Add("infcode", -1);                joRtn.Add("err_msg", "invokeCenterService(1):" + ex.Message);                return joRtn;            }        }        private string getResponseData(HttpWebResponse response)        {            string data = "";            if (response != null)            {                Stream s = response.GetResponseStream();                StreamReader sRead = new StreamReader(s);                data = sRead.ReadToEnd();                sRead.Close();                response.Close();            }            return data;        }        /// <summary>        /// 这个是下载目录用的invokeCenterService        /// </summary>        /// <param name="funNO"></param>        /// <param name="data"></param>        /// <returns></returns>        public JObject invokeCenterServiceApi(string funNO, string data)        {            JObject joRtn = new JObject();            int iInt = 0;            String outPar = ""; Boolean bDownLoad = false;            try            {                                Global.curEvt.URL = Global.inf.centerURL;                                              joRtn = invokeCenterService(data);                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            catch (Exception ex)            {                if (joRtn["infcode"] == null)                { joRtn.Add("infcode", -1); }                if (joRtn["err_msg"] == null)                { joRtn.Add("err_msg", "invokeCenterService(3):" + ex.Message); }                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            finally            {                Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), outPar);                                this.saveCenterLog(JsonHelper.Compress(data), outPar, JsonHelper.Compress(data), outPar);            }        }        /// <summary>        /// 调用银海COM组件invokeCenterService        /// </summary>        /// <param name="funNO"></param>        /// <param name="data"></param>        /// <returns></returns>        public JObject invokeCenterService(string funNO, JObject data)        {            JObject joRtn = new JObject();            string outPar = "";            try            {                int iRes = yh_hb_call(funNO, data.ToString(), out outPar);                if (iRes == 0)                {                    joRtn = JObject.Parse(outPar);                }                else                {                    joRtn.Add("infcode", -1);                    joRtn.Add("err_msg", outPar);                    outPar = JsonHelper.Compress(joRtn);                }                return joRtn;            }            catch (Exception ex)            {                if (joRtn["infcode"] == null)                { joRtn.Add("infcode", -1); }                if (joRtn["err_msg"] == null)                { joRtn.Add("err_msg", "invokeCenterService(COM):" + ex.Message); }                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            finally            {                Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), outPar);                this.saveCenterLog(JsonHelper.Compress(data), outPar, JsonHelper.Compress(data), outPar);            }        }        /// <summary>        /// 调用银海COM组件invokeCenterService        /// </summary>        /// <param name="funNO"></param>        /// <param name="data"></param>        /// <returns></returns>        public JObject invokeCenterService(string funNO, string data)        {            JObject joRtn = new JObject();                        string outPar = "";                        try            {                Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", "哈哈", "哈哈哈");                int iRes = yh_hb_call(funNO, data, out outPar);                if (funNO != "YH03")                 {                    if (iRes == 0)                    {                        joRtn = JObject.Parse(outPar);                    }                     else                    {                        joRtn.Add("infcode", -1);                        joRtn.Add("err_msg", outPar);                        outPar = JsonHelper.Compress(joRtn);                    }                }                else //打印交易不做处理                {                    joRtn.Add("infcode", 0);                    joRtn.Add("err_msg", "");                    outPar = JsonHelper.Compress(joRtn);                }                return joRtn;            }            catch (Exception ex)            {                if (joRtn["infcode"] == null)                { joRtn.Add("infcode", -1); }                if (joRtn["err_msg"] == null)                { joRtn.Add("err_msg", "invokeCenterService(COM):" + ex.Message); }                outPar = JsonHelper.Compress(joRtn);                return joRtn;            }            finally            {                Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), outPar);                this.saveCenterLog(JsonHelper.Compress(data), outPar, JsonHelper.Compress(data), outPar);            }        }        /// <summary>        /// 医保目录txt文件下载-湖北        /// </summary>        /// <param name="data"></param>        /// <returns></returns>        public string HBDownloadCenterFile(string data)        {            return "0";        }        /// <summary>        /// 医保目录txt文件下载        /// </summary>        /// <param name="data"></param>        /// <returns></returns>        public JObject DownloadCenterFile(string data)        {            string error = string.Empty; int errorCode = 0;            string sRtn = "";            try            {                JObject jsonInParam = JObject.Parse(data);                string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];                string filePath = Global.curEvt.path + "\\Download\\" + fileName;                //MessageBox.Show("9102下载文件入参:"+jsonInParam.ToString());                //如果不存在目录,则创建目录                if (!Directory.Exists(Global.curEvt.path + "\\Download"))                {                    //创建文件夹                    DirectoryInfo dirInfo = Directory.CreateDirectory(Global.curEvt.path + "\\Download");                }                if (File.Exists(filePath))                {                    File.Delete(filePath);                }                FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);                //创建一个HTTP请求                  //Global.curEvt.URL = Global.inf.centerURL + "/hsa-fsi-9102";                Global.curEvt.URL = Global.inf.centerURL;                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);                //Post请求方式                  request.Method = "POST";                //内容类型                request.ContentType = "application/json";                request.Headers.Add("apikey", "fa8kQMIF6LB0D7c6UGQoZc6TCcZO2G6O");                request.Accept = "text/html, application/xhtml+xml, */*";                /*                //昆明增加头部信息                string timestamp = TimeStamp.get13().ToString();  //当前时间戳(秒)                string nonce = Guid.NewGuid().ToString();         //非重复的随机字符串(十分钟内不能重复)                string InsuHosID = Global.inf.hospitalNO;         //医疗机构ID                string CreditID = Global.inf.CreditID;            //服务商统一社会信用代码                string BusinessID = Global.inf.BusinessID;        //服务商ID                                string sTemp = timestamp + BusinessID + nonce;                //Sha256 加密生成的签名 signature = sha256(hsf_timestamp + infosyssign + hsf_nonce)                string signature = Encrypt.SHA256EncryptStr(sTemp);                request.Headers.Add("hsf_signature", signature);                request.Headers.Add("hsf_timestamp", timestamp);                request.Headers.Add("hsf_nonce", nonce);                request.Headers.Add("fixmedins_code", InsuHosID);                request.Headers.Add("infosyscode", CreditID);                */                //设置参数,并进行URL编码                 string paraUrlCoded = data;//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.errorMessage = error;                joReturn.filePath = filePath;                sRtn = joReturn.ToString();                return joReturn;            }            catch (Exception ex)            {                errorCode = -100;                error = ex.Message;                dynamic joReturn = new JObject();                joReturn.errorCode = errorCode;                joReturn.errorMessage = error;                sRtn = joReturn.ToString();                return joReturn;            }            finally            {                Global.writeLog("DownloadCenterFile" +"(" + Global.curEvt.URL + ")", data, sRtn);            }        }        /// <summary>        /// 保存中心交易日志到数据库        /// </summary>        /// <param name="inParam"></param>        /// <param name="outParam"></param>        /// <param name="inParamPlain"></param>        /// <param name="outParamPlain"></param>        private void saveCenterLog(string inParam, string outParam, string inParamPlain, string outParamPlain)        {            dynamic joIris = new JObject();            string sRtn = "";            try            {                //解析postContent,插入医保交易日志表                JObject joIn = new JObject(JObject.Parse(inParam));                JObject joOut = new JObject(JObject.Parse(outParam));                JObject joInPlain = new JObject(JObject.Parse(inParamPlain));                JObject joOutPlain = new JObject(JObject.Parse(outParamPlain));                JArray jaParams = new JArray();                JObject joParam = new JObject();                joParam.Add("inParam", JObject.FromObject(joIn));                joParam.Add("outParam", JObject.FromObject(joOut));                joParam.Add("inParamPlain", JObject.FromObject(joInPlain));                joParam.Add("outParamPlain", JObject.FromObject(joOutPlain));                joParam.Add("HospitalDr", Global.inf.hospitalDr);                joParam.Add("InterfaceDr", Global.inf.interfaceDr);                joParam.Add("updateUserID", Global.user.ID);                joParam.Add("psn_no", Global.pat.psn_no);                jaParams.Add(joParam);                joIris.code = "09010021";                joIris.Add("params", jaParams);                //InvokeHelper invoker = new InvokeHelper();                sRtn = invokeInsuService(joIris.ToString(), "保存日志到数据库").ToString();            }            catch (Exception ex)            {                sRtn = JsonHelper.setExceptionJson(-100, "保存日志异常", ex.Message).ToString();                Global.writeLog_Iris("保存日志异常:" + sRtn.ToString());            }        }    }}
 |