using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Runtime.Serialization.Json; using System.Security.Cryptography; using System.Text; namespace Common { public class Tools { //厂商编码:ZHONGLIAN //厂商名称:中联 //AES密钥:fu7vttF6y4uzdy5L //校验码 / 授权码:ZLJR2022V //LogHelper.Info("医生编号"); public static string accessToken = null; public static string pwd = "fu7vttF6y4uzdy5L"; public static DateTime DateTimes; //public static string Time = "";//如互今日时间相等,则今日已上传,无需再上传 public static string FormatStr(string str) { return str.Replace("\n", "").Replace("\t", "").Replace("\r", ""); } public static string ReplaceString(string str) { return str.Replace("\\", "\\\\").Replace("\"", "\\\""); } /// /// 将指定文件转换成Base64字符串 /// /// 文件路径 /// 返回Base64字符串 public static string FileToBase64Str(string path) { string base64Str = string.Empty; try { // 空路径名是不合法的 if (string.IsNullOrEmpty(path)) { base64Str = null; return base64Str; } // 判断路径是否存在 if (!File.Exists(path)) { base64Str = null; return base64Str; } using (System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { byte[] bt = new byte[fileStream.Length]; // 调用read读取方法 fileStream.Read(bt, 0, bt.Length); base64Str = Convert.ToBase64String(bt); } } catch (Exception ex) { LogHelper.Error(ex); } return base64Str; } /// /// AES加密 对应java中的 aes/cbc/pkcs5padding 模式的算法 /// /// 待加密的字符串 /// 密钥 /// public static string AESEncrypt(string str, string Key, string IV) { try { byte[] keyArray = Encoding.UTF8.GetBytes(Key); byte[] toEncryptArray = Encoding.UTF8.GetBytes(str); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.CBC; rDel.Padding = PaddingMode.PKCS7; rDel.IV = Encoding.UTF8.GetBytes(IV); ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } catch (Exception ex) { LogHelper.Error(ex); } return ""; } /// /// AES解密 对应java中的 aes/cbc/pkcs5padding 模式的算法 /// /// 待解密的字符串 /// 密钥 /// 返回空为解析失败 public static string AESDecrypt(string str, string key, string IV) { try { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] encryptedData = Convert.FromBase64String(str); byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) len = keyBytes.Length; System.Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = Encoding.UTF8.GetBytes(IV); ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); return Encoding.UTF8.GetString(plainText); } catch (Exception ex) { LogHelper.Error(ex); } return ""; } /// /// 异步请求 /// /// /// //public static void GetDataAsync(string num, Action action) //{ // //这里需要的是一个string返回类型,int传入参数的委托 // Func func = WSCenterData; // AsyncCallback callback = ar => // { // try // { // action.Invoke(ar); // } // catch (Exception ex) // { // Log.Info("异常:" + ex.Message, true); // Log.Info("异常:" + ex.StackTrace, true); // } // }; // func.BeginInvoke(num, callback, null); //} public static string WSCenterData(string url,string data,string vi,string token = "",int sign = 0) { //Result result = new Result(); string srcString = string.Empty; //string data = "{\"KEY\":\"VALUE\",\"type\":\"JSON\",\"content\":" + str + "}"; //string url = "http://40.26.204.3:8084/phimp-napi/token"; try { Encoding myEncoding = Encoding.GetEncoding("UTF-8"); ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; //将提交的字符串数据转换成字节数组 //注意提交的编码,这里默认的是Default:系统当前编码 byte[] postData = myEncoding.GetBytes(data); //设置提交的相关参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.KeepAlive = false; request.ProtocolVersion = HttpVersion.Version10; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; //request.ContentType = "text/xml;charset=utf-8"; request.ContentType = "application/json;charset=utf-8"; request.Headers.Add("code", "ZHONGLIAN"); if (sign == 0) { string vcode = AESEncrypt("ZLJR2022V", pwd, vi); request.Headers.Add("validCode", vcode); } else { request.Headers.Add("accessToken", token); } request.Headers.Add("vi", vi); request.ContentLength = postData.Length; using (System.IO.Stream writer = request.GetRequestStream()) { writer.Write(postData, 0, postData.Length); } HttpWebResponse response = request.GetResponse() as HttpWebResponse; using (System.IO.Stream sr = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(sr, myEncoding)) { srcString = reader.ReadToEnd(); } } //result = JSON.JsonToObject(Tools.FormatStr(srcString), Encoding.UTF8); } catch (Exception ex) { LogHelper.Info("请求入参:" + data); LogHelper.Info("请求出参:" + srcString); LogHelper.Error(ex); } //Log.Info("入参:" + data, true); //Log.Info("出参:" + srcString, true); return srcString; } public static string WSDataToZlsoftInterface(string url, string data) { string srcString = string.Empty; try { Encoding myEncoding = Encoding.GetEncoding("UTF-8"); ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; //将提交的字符串数据转换成字节数组 //注意提交的编码,这里默认的是Default:系统当前编码 byte[] postData = myEncoding.GetBytes(data); //设置提交的相关参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.KeepAlive = false; request.ProtocolVersion = HttpVersion.Version10; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; //request.ContentType = "text/xml;charset=utf-8"; request.ContentType = "application/json;charset=utf-8"; request.ContentLength = postData.Length; using (System.IO.Stream writer = request.GetRequestStream()) { writer.Write(postData, 0, postData.Length); } HttpWebResponse response = request.GetResponse() as HttpWebResponse; using (System.IO.Stream sr = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(sr, myEncoding)) { srcString = reader.ReadToEnd(); } } //result = JSON.JsonToObject(Tools.FormatStr(srcString), Encoding.UTF8); } catch (Exception ex) { LogHelper.Info("请求入参:" + data); LogHelper.Info("请求出参:" + srcString); LogHelper.Error(ex); } //Log.Info("入参:" + data, true); //Log.Info("出参:" + srcString, true); return srcString; } public static string GuidTo16String() { return DateTime.Now.ToString("yyyyMMddHHmmssff"); } /// /// 实体类转换为JSON串 /// /// 实体类类型 /// 实体类实例 /// 编码 /// public static string ObjectToJson(Object jsonObject, Encoding encoding) { string result = String.Empty; DataContractJsonSerializer serializer = new DataContractJsonSerializer(jsonObject.GetType()); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { serializer.WriteObject(ms, jsonObject); result = encoding.GetString(ms.ToArray()); } return result; } /// /// JSON串转换为实体类 /// /// 实体类类型 /// JSON串 /// 编码 /// public static T JsonToObject(string json, Encoding encoding) { T resultObject = Activator.CreateInstance(); DataContractJsonSerializer serializer = new DataContractJsonSerializer(resultObject.GetType()); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(encoding.GetBytes(json))) { resultObject = (T)serializer.ReadObject(ms); } return resultObject; } } }