123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace DirectoryDownload
- {
- class InvokeMethod
- {
- public string Post(string Url, string jsonParas)
- {
- GlobalVariables.writeLog("URL:" + Url);
- GlobalVariables.writeLog("jsonParas:" + jsonParas);
- string strURL = Url;
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
-
- request.Method = "POST";
-
- request.ContentType = "application/json";
-
- string paraUrlCoded = jsonParas;
- byte[] payload;
-
- payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
-
- request.ContentLength = payload.Length;
-
- Stream writer;
- try
- {
- writer = request.GetRequestStream();
- }
- catch (Exception)
- {
- writer = null;
- GlobalVariables.writeLog("连接服务器失败!");
- }
-
- writer.Write(payload, 0, payload.Length);
- writer.Close();
-
- HttpWebResponse response;
- try
- {
-
- response = (HttpWebResponse)request.GetResponse();
- }
- catch (WebException ex)
- {
- response = ex.Response as HttpWebResponse;
- }
- Stream s = response.GetResponseStream();
-
- StreamReader sRead = new StreamReader(s);
- string postContent = sRead.ReadToEnd();
- sRead.Close();
-
- IrisServices iris = new IrisServices();
- dynamic joIris = new JObject();
- JObject joTmp = new JObject();
- joTmp.Add("In", jsonParas);
- joTmp.Add("out",postContent);
- joIris.code = "09010021";
- joIris.Add("params", JObject.FromObject(joTmp));
- iris.Invoke(joIris.ToString());
- return postContent;
- }
- public bool Download(string url, string strParams)
- {
- try
- {
- GlobalVariables.writeLog("strParams" + strParams);
- JObject jsonInParam = JObject.Parse(strParams);
- string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];
- GlobalVariables.writeLog("filename" + fileName);
- string filePath = System.Environment.CurrentDirectory + "\\" + fileName;
- if (File.Exists(filePath))
- {
- File.Delete(filePath);
- }
- FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
- string strURL = url;
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
-
- request.Method = "POST";
-
- request.ContentType = "application/json";
-
- string paraUrlCoded = strParams;
- byte[] payload;
-
- payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
-
- request.ContentLength = payload.Length;
- Stream writer;
- try
- {
- writer = request.GetRequestStream();
- }
- catch (Exception)
- {
- writer = null;
- Console.Write("连接服务器失败!");
- }
-
- writer.Write(payload, 0, payload.Length);
- writer.Close();
-
-
- HttpWebResponse response = request.GetResponse() as HttpWebResponse;
-
- Stream responseStream = response.GetResponseStream();
-
- byte[] bArr = new byte[1024];
- int iTotalSize = 0;
- int size = responseStream.Read(bArr, 0, (int)bArr.Length);
- while (size > 0)
- {
- iTotalSize += size;
- fs.Write(bArr, 0, size);
- size = responseStream.Read(bArr, 0, (int)bArr.Length);
- }
- fs.Close();
- responseStream.Close();
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
- }
- }
- class IrisServices
- {
- public string Invoke(string data)
- {
- GlobalVariables.writeLog("入参:" + data);
-
- string url = string.Format("{0}/{1}", GlobalVariables.irisServiceIP, GlobalVariables.irisServiceURL);
-
-
- ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
-
- HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
-
-
- byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
- myRequest.Method = "POST";
- myRequest.ContentLength = buf.Length;
- myRequest.ContentType = "application/json";
-
-
-
- myRequest.Headers.Add("Authorization", GlobalVariables.irisServiceAuthorization);
- myRequest.MaximumAutomaticRedirections = 1;
- myRequest.AllowAutoRedirect = true;
-
- Stream stream = myRequest.GetRequestStream();
- stream.Write(buf, 0, buf.Length);
- stream.Close();
-
-
- HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
-
- StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
-
- string rtn = reader.ReadToEnd();
- reader.Close();
- myResponse.Close();
- GlobalVariables.writeLog("出参:" + rtn);
- return rtn;
- }
- }
- class YinHaiCom
- {
- string progID1 = "YinHai.CHS.InterfaceSCS";
- System.Type YinHaiComType;
- object YinHaiComInstance;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int Init()
- {
- YinHaiComType = System.Type.GetTypeFromProgID(progID1);
-
- if (YinHaiComType != null)
- {
- GlobalVariables.writeLog("开始COM组件Init");
-
- YinHaiComInstance = Activator.CreateInstance(YinHaiComType);
- if (YinHaiComInstance != null)
- {
- GlobalVariables.writeLog("Init实例创建成功");
- }
-
- object[] ParamArray = new object[2];
- ParamArray[0] = 0;
- ParamArray[1] = "";
- ParameterModifier[] ParamMods = new ParameterModifier[1];
- ParamMods[0] = new ParameterModifier(2);
- ParamMods[0][0] = true;
- ParamMods[0][1] = true;
- YinHaiComType.InvokeMember("yh_CHS_init",
- BindingFlags.Default | BindingFlags.InvokeMethod,
- null,
- YinHaiComInstance,
- ParamArray,
- ParamMods,
- null,
- null);
- string Msg = "加载成功:" + ParamArray[1].ToString();
- GlobalVariables.writeLog(Msg + "___" + ParamArray[0].ToString());
- return (int)ParamArray[0];
- }
- else
- {
- string Msg = "YinHaiComType加载失败!";
- GlobalVariables.writeLog(Msg);
- return 1;
- }
- }
- public void Call(string infno,string input,out string output)
- {
- if (YinHaiComType != null)
- {
-
-
- if (YinHaiComInstance != null)
- {
- GlobalVariables.writeLog("实例创建成功,准备调用Call服务");
- }
- else
- {
- output = "实例不存在!";
- GlobalVariables.writeLog("实例不存在");
- return;
- }
- GlobalVariables.writeLog("入参infno:" + infno);
- GlobalVariables.writeLog("入参input:" + input);
-
- object[] ParamArray = new object[3];
- ParamArray[0] = infno;
- ParamArray[1] = input;
- ParamArray[2] = "";
- ParameterModifier[] ParamMods = new ParameterModifier[1];
- ParamMods[0] = new ParameterModifier(3);
-
-
- ParamMods[0][2] = true;
- YinHaiComType.InvokeMember("yh_CHS_call",
- BindingFlags.Default | BindingFlags.InvokeMethod,
- null,
- YinHaiComInstance,
- ParamArray,
- ParamMods,
- null,
- null);
- output = ParamArray[2].ToString();
- }
- else
- {
- output = "COM加载失败!";
- GlobalVariables.writeLog("COM加载失败!");
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- public void Destroy(out string output)
- {
-
- if (YinHaiComType != null)
- {
-
- YinHaiComInstance = Activator.CreateInstance(YinHaiComType);
- if (YinHaiComInstance != null)
- {
- GlobalVariables.writeLog("实例创建成功,准备调用Call服务");
- }
- else
- {
- output = "实例不存在!";
- GlobalVariables.writeLog("实例不存在");
- return;
- }
-
- object[] ParamArray = new object[0];
- ParameterModifier[] ParamMods = new ParameterModifier[0];
- ParamMods[0] = new ParameterModifier(0);
- YinHaiComType.InvokeMember("yh_CHS_destroy",
- BindingFlags.Default | BindingFlags.InvokeMethod,
- null,
- YinHaiComInstance,
- ParamArray,
- ParamMods,
- null,
- null);
- output = "destroy成功!";
- }
- else
- {
- output = "COM加载失败!";
- GlobalVariables.writeLog("COM加载失败!");
- }
- }
- }
- #region
- class DllInvoke
- {
- public static object ManagedInvoke(string DllFileName, string NameSpace, string ClassName, string MethodName, object[] ObjArrayParams)
- {
-
-
-
-
- object o = new object();
- string inMes = "";
- try
- {
-
- Assembly DllAssembly = Assembly.LoadFile(DllFileName);
-
- Type DllType = DllAssembly.GetType(NameSpace + "." + ClassName);
- if (DllType == null)
- {
- o = NameSpace + "." + ClassName + "未获取到DllType";
- return o;
- }
-
- 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 ex)
- {
- o =ex.ToString();
- if (ex.InnerException != null)
- {
- inMes = ex.InnerException.Message;
- o = inMes;
- }
- }
- }
- }
- catch (Exception ex)
- {
- o = ex.ToString();
- if (ex.InnerException != null)
- {
- inMes = ex.InnerException.Message;
- o = inMes.ToString();
- }
- }
-
- return o;
- }
- }
- #endregion
- }
|