| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using System.Net.Http.Headers; | 
| using WIDESEA_Core.LogHelper; | 
| using System.Security.Cryptography; | 
|   | 
| namespace WIDESEA_Core.Helper | 
| { | 
|     public class HttpMesHelper | 
|     { | 
|         public static string Post(string serviceAddress, string requestJson = null, string contentType = "application/json", Dictionary<string, string>? headers = null) | 
|         { | 
|             string result = string.Empty; | 
|             DateTime beginDate = DateTime.Now; | 
|             try | 
|             { | 
|                 using (HttpContent httpContent = new StringContent(requestJson)) | 
|                 { | 
|                     httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); | 
|                      | 
|                     using HttpClient httpClient = new HttpClient(); | 
|                     httpClient.Timeout = new TimeSpan(0, 0, 60); | 
|                     string authorization = "AppKey 1830415116987195392"; | 
|                     headers = new Dictionary<string, string> | 
|                     { | 
|                         { "Authorization", authorization }, | 
|                         { "Site_tenant_id", "0dc2d164-8a62-417e-8a00-808007464feb" } | 
|                     }; | 
|                     if (headers != null) | 
|                     { | 
|                         foreach (var header in headers) | 
|                             httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); | 
|                     } | 
|                     HttpResponseMessage responseMessage = httpClient.PostAsync(serviceAddress, httpContent).Result; | 
|                     result = responseMessage.Content.ReadAsStringAsync().Result; | 
|                 } | 
|                 return result; | 
|             } | 
|             catch (Exception e) | 
|             { | 
|                 throw new Exception(e.Message); | 
|             } | 
|             finally | 
|             { | 
|                 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; | 
|         } | 
|     } | 
| } |