/******************************************************************************
* 文件名称: 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 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 PTMedInsuInterface.Common;
using Newtonsoft.Json;
using PTMedInsuInterface.Variables;
namespace PTMedInsuInterface.Helper
{
class InvokeHelper
{
private string serviceURL;
private string authorization;
///
/// 封装业务DLL的调用,返回JSON对象
///
///
///
///
public JObject InvokeBusinessDLL(string methodName, string inParam)
{
string outParam = string.Empty;
try
{
DllInvoke dllInvoke = new DllInvoke();
string path = Global.curEvt.path + "\\" + Global.dll.name;
string[] str = new string[1];
str[0] = inParam;
object o2 = DllInvoke.ManagedInvoke(path,Global.dll.nameSpace, "InsuBusiness", methodName, str);
outParam = o2.ToString();
JObject joRtn = JObject.Parse(outParam);
return joRtn;
}
catch (Exception ex)
{
outParam = "InvokeBusinessDLL:" + outParam + "\r\n" + ex.Message;
return JsonHelper.setExceptionJson(-1, "businessDLLInvoke:" , outParam);
}
finally
{
Global.writeLog("InvokeBusinessDLL." + methodName, inParam, outParam);
}
}
///
/// iris服务调用的封装
///
///
///
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;
}
}
///
/// HIS服务调用的封装
///
///
///
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(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), JsonHelper.Compress(joRtn));
}
}
///
/// 医保平台服务调用的封装
///
///
///
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);
return joRtn;
}
catch (Exception ex)
{
joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message );
rtn = JsonConvert.SerializeObject(joRtn);
return joRtn;
}
finally
{
Global.writeLog(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), rtn);
}
}
}
}