星火直播PC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataProvider.cs 3.5KB

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