| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- using Microsoft.Extensions.Configuration;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Net.Sockets;
- using System.Text;
- namespace ConvertLog.common
- {
- public class Common
- {
- static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Common));
- private readonly IConfiguration _config;
- public Common(IConfiguration configuration)
- {
- _config = configuration;
-
- string ConnectionString = _config["Configurations:ConnectionString"];
- }
- public string GetValuesAppSetting(string key, string subkey)
- {
- string reuturn = "";
- string _key = key + ":" + subkey;
- reuturn = _config[_key];
- return reuturn;
- }
- public static string SocketUnSyn(string msg, string host, int port, int timeout)
- {
- //log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
- //log4net.Config.XmlConfigurator.Configure();
- string result = "-1000";
- TcpClient client = null;
- NetworkStream stream = null;
- try
- {
- byte[] data = System.Text.Encoding.ASCII.GetBytes(msg);
- client = new TcpClient(host, port);
- client.SendTimeout = timeout;
- stream = client.GetStream();
- BinaryWriter writer = new BinaryWriter(stream);
- writer.Write(data);
- String _readFromStream = String.Empty;
- byte[] data_read = new byte[1024];
- using (MemoryStream ms = new MemoryStream())
- {
- int numBytesRead;
- while ((numBytesRead = stream.Read(data_read, 0, data_read.Length)) > 0)
- {
- ms.Write(data_read, 0, numBytesRead);
- }
- _readFromStream = System.Text.Encoding.ASCII.GetString(ms.ToArray(), 0, (int)ms.Length);
- }
- //string _readFromStream = GetResponse(stream);
- /*
- data = new Byte[1024];
- String responseData = String.Empty;
- Int32 bytes = stream.Read(data, 0, data.Length);
- responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
- */
- logger.Info("Response from:" + host + ":" + port + " is:" + _readFromStream);
- // Close everything.
- stream.Close();
- client.Close();
- //Phan tich data tra ve:
- result = _readFromStream;
- }
- catch (ArgumentNullException e)
- {
- logger.Info("ArgumentNullException and close connection:" + e.ToString());
- stream.Close();
- client.Close();
- return "-1000";
- }
- catch (SocketException e)
- {
- return "-1000";
- }
- return result;
- }
- public static string GetResponse(NetworkStream stream)
- {
- byte[] data = new byte[1024];
- using (MemoryStream memoryStream = new MemoryStream())
- {
- do
- {
- stream.Read(data, 0, data.Length);
- memoryStream.Write(data, 0, data.Length);
- } while (stream.DataAvailable);
- return System.Text.Encoding.ASCII.GetString(memoryStream.ToArray(), 0, (int)memoryStream.Length);
- }
- }
- public static string checkToken(string ip, string channel, string token, out string msisdn, out string message)
- {
- string result = "1"; //check token ko thanh cong
- msisdn = "";
- message = "";
- try
- {
- //DataSet ds_check = usersDataAccess.TKcheckToken(ip, channel, token);
- DataSet ds_check =null;
- if (ds_check != null & ds_check.Tables[0].Rows.Count > 0)
- {
- msisdn = ds_check.Tables[0].Rows[0]["msisdn"].ToString();
- result = ds_check.Tables[0].Rows[0]["status"].ToString();
- message = ds_check.Tables[0].Rows[0]["mesage"].ToString();
- }
- }
- catch (Exception ex)
- {
- int a = 1;
- }
- return result;
- }
- public static string GetErrMessageFromErrCode(string errCode, string language, string url, string _keyPost, string channel)
- {
- Random _rd = new Random();
- string strRandom = _rd.Next(1, 999999).ToString("000000");
- string request_process = strRandom + DateTime.Now.ToString("ddMMyyyyhhmmssfff");
- string jsomPost = "{\"requestId\":\"#?rqeustID#\",\"serviceId\":\"10\",\"key\":\"#?key# \",\"cmd\":\"getErrCodeToMessage\",\"language\":\"#?language#\",\"channel\":\"#?channel#\",\"data\":\"#?errCode#\"}";
- jsomPost = jsomPost.Replace("#?rqeustID#", request_process);
- jsomPost = jsomPost.Replace("#?key#", _keyPost);
- jsomPost = jsomPost.Replace("#?language#", language);
- jsomPost = jsomPost.Replace("#?errCode#", errCode);
- jsomPost = jsomPost.Replace("#?channel#", channel);
- string strResponse = Common.SendPost(url, jsomPost);
- //ResCommnon response = JsonConvert.DeserializeObject<ResCommnon>(strResponse);
- //return response.responseMessage;
- return "";
- }
- public static String SendPost(string url, string str)
- {
- var data = new StringContent(str, Encoding.UTF8, "application/json");
- //StringBuilder sb = new StringBuilder();
- //sb.Append("Request: " + data);
- using (var client = new HttpClient())
- {
- var response = client.PostAsync(url, data).Result;
- if (response.IsSuccessStatusCode)
- {
- var responseContent = response.Content;
- //sb.Append("\r\nResponse: " + responseContent);
- //log.Debug(sb.ToString());
- // by calling .Result you are synchronously reading the result
- string responseString = responseContent.ReadAsStringAsync().Result;
- return responseString;
- }
- //sb.Append("\r\nResponse: " + response);
- //log.Debug(sb.ToString());
- return response.StatusCode.ToString();
- }
- }
-
- public static bool CheckPortV2(string ip, string port)
- {
- bool result = false;
- try
- {
- TcpClient c = new TcpClient(ip, Convert.ToInt32(port));
- result = true;
- c.Close();
- }
- catch (System.Net.Sockets.SocketException ex)
- {
- }
- return result;
- }
- public static bool CheckPort(string ip, string port)
- {
- bool result = false;
- string hostname = ip;
- int portno = Convert.ToInt32(port);
- IPAddress ipa = (IPAddress)Dns.GetHostAddresses(hostname)[0];
- try
- {
- System.Net.Sockets.Socket sock =
- new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
- System.Net.Sockets.SocketType.Stream,
- System.Net.Sockets.ProtocolType.Tcp);
- sock.Connect(ipa, portno);
- if (sock.Connected == true) // Port is in use and connection is successful
- result = true;
- sock.Close();
- }
- catch (System.Net.Sockets.SocketException ex)
- {
- }
- return result;
- }
-
- }
- }
|