| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- using Newtonsoft.Json.Linq;
- using PTMedicalInsurance.Common;
- using PTMedicalInsurance.Variables;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Data;
- namespace PTMedicalInsurance.Helper
- {
- class InvokeRestCenter : IInvokeCenter
- {
- private static string GetRandomString(int length)
- {
- const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- var random = new Random();
- return new string(Enumerable.Repeat(chars, length)
- .Select(s => s[random.Next(s.Length)]).ToArray());
- }
- private static long GetCurrentUnixSeconds()
- {
- DateTimeOffset utcNow = DateTimeOffset.UtcNow.AddHours(8);
- return (long)(utcNow.ToUniversalTime().DateTime.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))).TotalSeconds;
- }
- private static string GetSHA256Str(string input)
- {
- using (SHA256 sha256Hash = SHA256.Create())
- {
- byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
- StringBuilder builder = new StringBuilder();
- foreach (byte b in bytes)
- {
- builder.Append(b.ToString("x2"));
- }
- return builder.ToString();
- }
- }
- public int Business(string inputData, ref string outputData)
- {
- outputData = "";
-
- JObject joRtn = new JObject();
- try
- {
- string _api_timestamp = TimeStamp.get13().ToString(); //当前时间戳(秒)
- string _api_name = Global.inf.apiName;
- string _api_version = "1.0.0";
- string _api_access_key = Global.inf.AK;
- string secretKey = Global.inf.SK;
- //签名
- string sTemp = "_api_access_key=" + _api_access_key + "&_api_name=" + _api_name + "&_api_timestamp=" + _api_timestamp + "&_api_version=" + _api_version;
- string _api_signature = Encrypt.ToBase64hmac(sTemp, secretKey);
- Global.writeLog($"_api_timestamp:{_api_timestamp}\n_api_name:{_api_name}\n_api_version:{"1.0.0"}" +
- $"\n_api_access_key:{_api_access_key}\nsecretKey:{secretKey}\n_api_signature:{_api_signature}" +
- $"\nurl:{Global.curEvt.URL}");
-
- //创建一个HTTP请求
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);
- //Post请求方式
- request.Method = "POST";
- //内容类型
- request.ContentType = "application/json";
- //增加头部信息
- request.Headers.Add("_api_timestamp", _api_timestamp);
- request.Headers.Add("_api_name", _api_name);
- request.Headers.Add("_api_version", _api_version);
- request.Headers.Add("_api_access_key", _api_access_key);
- request.Headers.Add("_api_signature", _api_signature);
- //设置参数,并进行URL编码
- string paraUrlCoded = inputData;
- 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();//关闭请求流
- HttpWebResponse response = null;
- try
- {
- //获得响应流
- response = (HttpWebResponse)request.GetResponse();
- }
- catch (WebException ex)
- {
- HttpWebResponse res = (HttpWebResponse)ex.Response;
- Stream myResponseStream = res.GetResponseStream();
- StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
- string retString = myStreamReader.ReadToEnd();
- outputData = JsonHelper.setExceptionJson(-99, "异常返回", retString).ToString();
- return -1;
- }
- outputData = getResponseData(response);
- joRtn = JObject.Parse(outputData);//返回Json数据
- return 0;
- }
- catch (Exception ex)
- {
- joRtn.Add("infcode", -1);
- joRtn.Add("err_msg", "调用中心服务异常invokeCenterService(1):" + ex.Message);
- outputData = JsonHelper.toJsonString(joRtn);
- return -1;
- }
- }
- public int BusinessExt(string inputData, ref string outputData)
- {
- return this.Business(inputData, ref outputData);
- }
- public int DownloadFile(string inputData, ref string outputData)
- {
- outputData = "";
- string error = string.Empty; int errorCode = 0;
- try
- {
- JObject jsonInParam = JObject.Parse(inputData);
- // 去除外wrapper层便于通用
- Utils.removeWrapper(jsonInParam);
- string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];
- string filePath = Global.curEvt.path + "\\Download\\" + fileName;
- //如果不存在目录,则创建目录
- 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请求
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);
- //Post请求方式
- request.Method = "POST";
- //内容类型
- request.ContentType = "application/json";
- string timestamp = TimeStamp.get13().ToString(); //当前时间戳(秒)
- string name = Global.inf.apiName;
- string version = "1.0.0";
- string access_key = Global.inf.AK;
- string secretKey = Global.inf.SK;
- //签名
- string sTemp = "_api_access_key=" + access_key + "&_api_name=" + name + "&_api_timestamp=" + timestamp + "&_api_version=" + version;
- string signature = Encrypt.ToBase64hmac(sTemp, Global.inf.SK);
- Global.writeLog($"_api_timestamp:{timestamp}\n_api_name:{Global.inf.apiName}\n_api_version:{"1.0.0"}\n_api_access_key:{Global.inf.AK}\n_api_signature:{signature}\nurl:{Global.curEvt.URL}");
- //增加头部信息
- request.Headers.Add("_api_timestamp", timestamp);
- request.Headers.Add("_api_name", Global.inf.apiName);
- request.Headers.Add("_api_version", "1.0.0");
- request.Headers.Add("_api_access_key", Global.inf.AK);
- request.Headers.Add("_api_signature", signature);
- //设置参数,并进行URL编码
- string paraUrlCoded = JsonHelper.toJsonString(jsonInParam);
- 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[102400];
- 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;
- outputData = joReturn.ToString();
- }
- catch (Exception ex)
- {
- errorCode = -100;
- error = ex.Message;
- dynamic joReturn = new JObject();
- joReturn.errorCode = errorCode;
- joReturn.errorMessage = error;
- outputData = joReturn.ToString();
- return -1;
- }
- finally
- {
- Global.writeLog("DownloadCenterFile" + "(" + Global.curEvt.URL + ")", inputData, outputData);
- }
- return 0;
- }
- public int Init(ref string pErrMsg)
- {
- return 0;
- }
- public int UploadFile(string inputData, ref string outputData, ref string pErrMsg)
- {
- throw new NotImplementedException();
- }
- private string getResponseData(HttpWebResponse response)
- {
- string data = "";
- if (response != null)
- {
- Stream s = response.GetResponseStream();
- StreamReader sRead = new StreamReader(s,Encoding.GetEncoding("UTF-8"));
- data = sRead.ReadToEnd();
- sRead.Close();
- response.Close();
- }
- return data;
- }
- }
- }
|