星火批注
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DataProvider.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. namespace Common.system
  9. {
  10. public class DataProvider
  11. {
  12. ///3DES解密
  13. public static string TripleDESDecrypt(string pToDecrypt, string sKey)
  14. {
  15. sKey = sKey.Substring(0, 24);
  16. TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider
  17. {
  18. Mode = CipherMode.ECB,
  19. Padding = PaddingMode.Zeros
  20. };
  21. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  22. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  23. {
  24. int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  25. inputByteArray[x] = (byte)i;
  26. }
  27. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  28. des.IV = new byte[] { 0, 00, 00, 00, 00, 00, 00, 00, };
  29. MemoryStream ms = new MemoryStream();
  30. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  31. cs.Write(inputByteArray, 0, inputByteArray.Length);
  32. cs.FlushFinalBlock();
  33. StringBuilder ret = new StringBuilder();
  34. return System.Text.Encoding.Default.GetString(ms.ToArray()).Replace("\0", "");
  35. }
  36. /// <summary>
  37. /// 返回一个时间戳到秒
  38. /// </summary>
  39. /// <returns></returns>
  40. public static long TimestampTotalSeconds()
  41. {
  42. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  43. long timestr = Convert.ToInt64(ts.TotalSeconds);
  44. return timestr;
  45. }
  46. /// <summary>
  47. /// 将字典类型序列化为json字符串
  48. /// </summary>
  49. /// <typeparam name="TKey">字典key</typeparam>
  50. /// <typeparam name="TValue">字典value</typeparam>
  51. /// <param name="dict">要序列化的字典数据</param>
  52. /// <returns>json字符串</returns>
  53. public static string SerializeDictionaryToJsonString<TKey, TValue>(Dictionary<TKey, TValue> dict)
  54. {
  55. if (dict.Count == 0)
  56. {
  57. return "";
  58. }
  59. string jsonStr = JsonConvert.SerializeObject(dict);
  60. return jsonStr;
  61. }
  62. /// <summary>
  63. ///调用Post接口
  64. /// </summary>
  65. /// <param name="request"></param>
  66. /// <returns></returns>
  67. public static string HttpPost(string body, string Url)
  68. {
  69. try
  70. {
  71. Encoding encoding = Encoding.UTF8;
  72. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
  73. request.Method = "POST";
  74. request.Accept = "text/html,application/xhtml+xml,*/*";
  75. request.ContentType = "application/json";
  76. byte[] buffer = encoding.GetBytes(body);
  77. request.ContentLength = buffer.Length;
  78. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  79. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  80. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  81. {
  82. return reader.ReadToEnd();
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. // LogHelper.Instance.Debug($"POST接口连接失败,请求参数:{ex.Message}");
  88. return "POST报错:" + ex.Message;
  89. }
  90. }
  91. }
  92. }