PosRePushHelper.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Linq;
  3. using System.Data;
  4. using System.Threading;
  5. using Library;
  6. using LitJson;
  7. using MySystem.Models.Push;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.IO;
  11. using System.Net;
  12. namespace MySystem
  13. {
  14. public class PosRePushHelper
  15. {
  16. public readonly static PosRePushHelper Instance = new PosRePushHelper();
  17. private PosRePushHelper()
  18. {
  19. }
  20. public void Start()//启动
  21. {
  22. Thread thread = new Thread(threadStart);
  23. thread.IsBackground = true;
  24. thread.Start();
  25. }
  26. private void threadStart()
  27. {
  28. while (true)
  29. {
  30. try
  31. {
  32. DoSomeThing();
  33. }
  34. catch (Exception ex)
  35. {
  36. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "重推机具数据异常");
  37. }
  38. Thread.Sleep(5000);
  39. }
  40. }
  41. //要执行的方法
  42. public void DoSomeThing()
  43. {
  44. string url = "";
  45. WebCMSEntities db = new WebCMSEntities();
  46. DateTime check = DateTime.Now.AddMinutes(-3);
  47. List<PushPosRecord> list = db.PushPosRecord.Where(m => m.CreateDate >= check).OrderBy(m => m.Id).ToList();
  48. foreach(PushPosRecord sub in list)
  49. {
  50. string dataType = sub.DataType;
  51. if(dataType == "bind") url = "http://gateway.kexiaoshuang.com/v1/kxs/statServer/sp/binding";
  52. if(dataType == "un_bind") url = "http://gateway.kexiaoshuang.com/v1/kxs/statServer/sp/unbind";
  53. if(dataType == "trade") url = "http://gateway.kexiaoshuang.com/v1/kxs/statServer/sp/trade";
  54. if(dataType == "deposit") url = "http://gateway.kexiaoshuang.com/v1/kxs/statServer/sp/cash";
  55. if(dataType == "active") url = "http://gateway.kexiaoshuang.com/v1/kxs/statServer/sp/extActData";
  56. string res = PostWebRequest(url, sub.EncryptContent, new Dictionary<string, string>());
  57. if(res.Contains("\"status\""))
  58. {
  59. JsonData backObj = JsonMapper.ToObject(res);
  60. if(backObj["status"].ToString() == "1")
  61. {
  62. PushPosRecord edit = db.PushPosRecord.FirstOrDefault(m => m.Id == sub.Id);
  63. if(edit != null)
  64. {
  65. db.PushPosRecord.Remove(edit);
  66. }
  67. }
  68. }
  69. }
  70. db.Dispose();
  71. }
  72. public void SaveToDb(string content, string encryptContent, string dataType)
  73. {
  74. WebCMSEntities db = new WebCMSEntities();
  75. db.PushPosRecord.Add(new PushPosRecord()
  76. {
  77. CreateDate = DateTime.Now,
  78. Content = content,
  79. DataType = dataType,
  80. EncryptContent = encryptContent,
  81. });
  82. db.SaveChanges();
  83. db.Dispose();
  84. }
  85. public string AesEncrypt(string str)
  86. {
  87. if (string.IsNullOrEmpty(str)) return null;
  88. Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
  89. string key = "CBTU1dD4Kd5pyiGWTsI10jRQ3SvKusSV";
  90. string iv = "DYgjCEIMVrj2W9xN";
  91. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  92. {
  93. Key = Encoding.UTF8.GetBytes(key),
  94. IV = Encoding.UTF8.GetBytes(iv),
  95. Mode = System.Security.Cryptography.CipherMode.CBC,
  96. Padding = System.Security.Cryptography.PaddingMode.PKCS7
  97. };
  98. System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
  99. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  100. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  101. }
  102. public string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> headers)
  103. {
  104. string ret = string.Empty;
  105. try
  106. {
  107. byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData);
  108. // 设置提交的相关参数
  109. HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
  110. System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
  111. request.Method = "POST";
  112. request.KeepAlive = false;
  113. request.AllowAutoRedirect = true;
  114. request.ContentType = "application/json";
  115. foreach (string key in headers.Keys)
  116. {
  117. request.Headers.Add(key, headers[key]);
  118. }
  119. request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
  120. request.ContentLength = postData.Length;
  121. // 提交请求数据
  122. Stream outputStream = request.GetRequestStream();
  123. outputStream.Write(postData, 0, postData.Length);
  124. outputStream.Close();
  125. HttpWebResponse response;
  126. Stream responseStream;
  127. StreamReader reader;
  128. string srcString;
  129. response = request.GetResponse() as HttpWebResponse;
  130. responseStream = response.GetResponseStream();
  131. reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
  132. srcString = reader.ReadToEnd();
  133. ret = srcString; //返回值赋值
  134. reader.Close();
  135. }
  136. catch (WebException ex)
  137. {
  138. HttpWebResponse response = (HttpWebResponse)ex.Response;
  139. Stream myResponseStream = response.GetResponseStream();
  140. //获取响应内容
  141. StreamReader myStreamReader = new StreamReader(myResponseStream);
  142. ret = myStreamReader.ReadToEnd();
  143. myResponseStream.Close();
  144. }
  145. catch (Exception ex)
  146. {
  147. ret = "fail";
  148. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "POST请求异常");
  149. }
  150. return ret;
  151. }
  152. }
  153. }