Common.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using Microsoft.Extensions.Configuration;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.IO;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. namespace ConvertLog.common
  12. {
  13. public class Common
  14. {
  15. static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Common));
  16. private readonly IConfiguration _config;
  17. public Common(IConfiguration configuration)
  18. {
  19. _config = configuration;
  20. string ConnectionString = _config["Configurations:ConnectionString"];
  21. }
  22. public string GetValuesAppSetting(string key, string subkey)
  23. {
  24. string reuturn = "";
  25. string _key = key + ":" + subkey;
  26. reuturn = _config[_key];
  27. return reuturn;
  28. }
  29. public static string SocketUnSyn(string msg, string host, int port, int timeout)
  30. {
  31. //log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  32. //log4net.Config.XmlConfigurator.Configure();
  33. string result = "-1000";
  34. TcpClient client = null;
  35. NetworkStream stream = null;
  36. try
  37. {
  38. byte[] data = System.Text.Encoding.ASCII.GetBytes(msg);
  39. client = new TcpClient(host, port);
  40. client.SendTimeout = timeout;
  41. stream = client.GetStream();
  42. BinaryWriter writer = new BinaryWriter(stream);
  43. writer.Write(data);
  44. String _readFromStream = String.Empty;
  45. byte[] data_read = new byte[1024];
  46. using (MemoryStream ms = new MemoryStream())
  47. {
  48. int numBytesRead;
  49. while ((numBytesRead = stream.Read(data_read, 0, data_read.Length)) > 0)
  50. {
  51. ms.Write(data_read, 0, numBytesRead);
  52. }
  53. _readFromStream = System.Text.Encoding.ASCII.GetString(ms.ToArray(), 0, (int)ms.Length);
  54. }
  55. //string _readFromStream = GetResponse(stream);
  56. /*
  57. data = new Byte[1024];
  58. String responseData = String.Empty;
  59. Int32 bytes = stream.Read(data, 0, data.Length);
  60. responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
  61. */
  62. logger.Info("Response from:" + host + ":" + port + " is:" + _readFromStream);
  63. // Close everything.
  64. stream.Close();
  65. client.Close();
  66. //Phan tich data tra ve:
  67. result = _readFromStream;
  68. }
  69. catch (ArgumentNullException e)
  70. {
  71. logger.Info("ArgumentNullException and close connection:" + e.ToString());
  72. stream.Close();
  73. client.Close();
  74. return "-1000";
  75. }
  76. catch (SocketException e)
  77. {
  78. return "-1000";
  79. }
  80. return result;
  81. }
  82. public static string GetResponse(NetworkStream stream)
  83. {
  84. byte[] data = new byte[1024];
  85. using (MemoryStream memoryStream = new MemoryStream())
  86. {
  87. do
  88. {
  89. stream.Read(data, 0, data.Length);
  90. memoryStream.Write(data, 0, data.Length);
  91. } while (stream.DataAvailable);
  92. return System.Text.Encoding.ASCII.GetString(memoryStream.ToArray(), 0, (int)memoryStream.Length);
  93. }
  94. }
  95. public static string checkToken(string ip, string channel, string token, out string msisdn, out string message)
  96. {
  97. string result = "1"; //check token ko thanh cong
  98. msisdn = "";
  99. message = "";
  100. try
  101. {
  102. //DataSet ds_check = usersDataAccess.TKcheckToken(ip, channel, token);
  103. DataSet ds_check =null;
  104. if (ds_check != null & ds_check.Tables[0].Rows.Count > 0)
  105. {
  106. msisdn = ds_check.Tables[0].Rows[0]["msisdn"].ToString();
  107. result = ds_check.Tables[0].Rows[0]["status"].ToString();
  108. message = ds_check.Tables[0].Rows[0]["mesage"].ToString();
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. int a = 1;
  114. }
  115. return result;
  116. }
  117. public static string GetErrMessageFromErrCode(string errCode, string language, string url, string _keyPost, string channel)
  118. {
  119. Random _rd = new Random();
  120. string strRandom = _rd.Next(1, 999999).ToString("000000");
  121. string request_process = strRandom + DateTime.Now.ToString("ddMMyyyyhhmmssfff");
  122. string jsomPost = "{\"requestId\":\"#?rqeustID#\",\"serviceId\":\"10\",\"key\":\"#?key# \",\"cmd\":\"getErrCodeToMessage\",\"language\":\"#?language#\",\"channel\":\"#?channel#\",\"data\":\"#?errCode#\"}";
  123. jsomPost = jsomPost.Replace("#?rqeustID#", request_process);
  124. jsomPost = jsomPost.Replace("#?key#", _keyPost);
  125. jsomPost = jsomPost.Replace("#?language#", language);
  126. jsomPost = jsomPost.Replace("#?errCode#", errCode);
  127. jsomPost = jsomPost.Replace("#?channel#", channel);
  128. string strResponse = Common.SendPost(url, jsomPost);
  129. //ResCommnon response = JsonConvert.DeserializeObject<ResCommnon>(strResponse);
  130. //return response.responseMessage;
  131. return "";
  132. }
  133. public static String SendPost(string url, string str)
  134. {
  135. var data = new StringContent(str, Encoding.UTF8, "application/json");
  136. //StringBuilder sb = new StringBuilder();
  137. //sb.Append("Request: " + data);
  138. using (var client = new HttpClient())
  139. {
  140. var response = client.PostAsync(url, data).Result;
  141. if (response.IsSuccessStatusCode)
  142. {
  143. var responseContent = response.Content;
  144. //sb.Append("\r\nResponse: " + responseContent);
  145. //log.Debug(sb.ToString());
  146. // by calling .Result you are synchronously reading the result
  147. string responseString = responseContent.ReadAsStringAsync().Result;
  148. return responseString;
  149. }
  150. //sb.Append("\r\nResponse: " + response);
  151. //log.Debug(sb.ToString());
  152. return response.StatusCode.ToString();
  153. }
  154. }
  155. public static bool CheckPortV2(string ip, string port)
  156. {
  157. bool result = false;
  158. try
  159. {
  160. TcpClient c = new TcpClient(ip, Convert.ToInt32(port));
  161. result = true;
  162. c.Close();
  163. }
  164. catch (System.Net.Sockets.SocketException ex)
  165. {
  166. }
  167. return result;
  168. }
  169. public static bool CheckPort(string ip, string port)
  170. {
  171. bool result = false;
  172. string hostname = ip;
  173. int portno = Convert.ToInt32(port);
  174. IPAddress ipa = (IPAddress)Dns.GetHostAddresses(hostname)[0];
  175. try
  176. {
  177. System.Net.Sockets.Socket sock =
  178. new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
  179. System.Net.Sockets.SocketType.Stream,
  180. System.Net.Sockets.ProtocolType.Tcp);
  181. sock.Connect(ipa, portno);
  182. if (sock.Connected == true) // Port is in use and connection is successful
  183. result = true;
  184. sock.Close();
  185. }
  186. catch (System.Net.Sockets.SocketException ex)
  187. {
  188. }
  189. return result;
  190. }
  191. }
  192. }