| | |
| | | { |
| | | public class HttpMesHelper |
| | | { |
| | | public static string Post(string serviceAddress, string requestJson = null, string contentType = "application/json", Dictionary<string, string>? headers = null) |
| | | public static string Post(string serviceAddress, string requestJson = "", string contentType = "application/json", Dictionary<string, string>? headers = null) |
| | | { |
| | | string result = string.Empty; |
| | | DateTime beginDate = DateTime.Now; |
| | |
| | | httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); |
| | | |
| | | using HttpClient httpClient = new HttpClient(); |
| | | httpClient.Timeout = new TimeSpan(0, 0, 60); |
| | | //æ£å¼ |
| | | string authorization = "AppKey 1830415116987195392"; |
| | | //æµè¯ |
| | | //string authorization = "AppKey 1773317109539201024"; |
| | | |
| | | |
| | | httpClient.Timeout = new TimeSpan(0, 0, 30); |
| | | string LoginToken = AppSettings.Get("MESLoginToken"); |
| | | headers = new Dictionary<string, string> |
| | | { |
| | | { "Authorization", authorization }, |
| | | //æ£å¼ |
| | | { "Site_tenant_id", "0dc2d164-8a62-417e-8a00-808007464feb" } |
| | | { "LoginToken", LoginToken } |
| | | //æµè¯ |
| | | //{ "Site_tenant_id", "ced19269-2b83-4577-be43-8cc2f700251e" } |
| | | //{ "LoginToken", "Z6_BusModel_LLD:5_92e848cf-b0b8-4f7e-9ac4-41e015c1fca3" } |
| | | }; |
| | | if (headers != null) |
| | | { |
| | |
| | | { |
| | | Logger.Add(serviceAddress, requestJson == null ? "" : requestJson, result, beginDate); |
| | | } |
| | | } |
| | | public static string GetStr(string textToEncrypt) |
| | | { |
| | | string publicKey; |
| | | string privateKey; |
| | | GenerateRSAKeyPair(out publicKey, out privateKey); |
| | | byte[] encryptedBytes = RSAEncrypt(textToEncrypt, publicKey); |
| | | // å¯ä»¥å°å å¯åçåèæ°ç»è½¬æ¢ä¸ºBase64å符串æ¹ä¾¿åç»å¤çï¼ä¾å¦ä¼ è¾ç |
| | | string encryptedBase64 = Convert.ToBase64String(encryptedBytes); |
| | | return encryptedBase64; |
| | | } |
| | | public static void GenerateRSAKeyPair(out string publicKey, out string privateKey) |
| | | { |
| | | using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048)) |
| | | { |
| | | publicKey = rsa.ToXmlString(false); |
| | | privateKey = rsa.ToXmlString(true); |
| | | } |
| | | } |
| | | public static byte[] RSAEncrypt(string plainText, string publicKey) |
| | | { |
| | | byte[] encryptedData; |
| | | using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) |
| | | { |
| | | rsa.FromXmlString(publicKey); |
| | | // å°ææ¬è½¬æ¢ä¸ºUTF8ç¼ç çåèæ°ç» |
| | | byte[] plainBytes = Encoding.UTF8.GetBytes(plainText); |
| | | // 使ç¨SHA1åå¸ç®æ³ä»¥åPkcs1å¡«å
è§åè¿è¡å å¯ |
| | | encryptedData = rsa.Encrypt(plainBytes, true); |
| | | } |
| | | return encryptedData; |
| | | } |
| | | } |
| | | } |