using System; using System.Data.SqlClient; using Newtonsoft.Json.Linq; using RestSharp; using System.Collections; namespace LogSendCSharp { class Program { static void Main(string[] args) { Console.WriteLine("Smart Facory Log Send Program"); DBConnection(); } static void Post(string url,string message,out string code,out string rslt) { var client = new RestClient(url); var request = new RestRequest(); request.AddHeader("Content-Type", "application/x-www-form-urlencoded"); request.AddBody(message); request.Method = Method.Post; var response = client.Execute(request); //Console.WriteLine(response.Content); JObject jobj = JObject.Parse(response.Content); code = jobj.GetValue("result")["recptnRsltCd"].ToString(); rslt = jobj.GetValue("result")["recptnRslt"].ToString(); } static void DBConnection() { string strConn = "Data Source=signus-smes.koreacentral.cloudapp.azure.com,14443;Initial Catalog=U3SMESHV;User ID=hvsmes;Password=u3hvsmes;"; SqlConnection conn = null; ArrayList arrList = new ArrayList(); try { conn = new SqlConnection(strConn); conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select * from T_SYS_LOG_SF WHERE SEND_YN= 'N' ORDER BY idx"; SqlDataReader rdr = cmd.ExecuteReader(); JArray jarr = new JArray(); int idx = 0; while (rdr.Read()) { JObject obj = new JObject(); idx = int.Parse(rdr["idx"].ToString()); arrList.Add(idx); string crtfcKey = rdr["crtfcKey"].ToString(); string logDt = rdr["logDt"].ToString(); string useSe = rdr["useSe"].ToString(); string sysUser = rdr["sysUser"].ToString(); string conectIp = rdr["conectIp"].ToString(); string dataUsgqty = rdr["dataUsgqty"].ToString(); obj.Add("crtfcKey", crtfcKey); obj.Add("logDt", logDt); obj.Add("useSe", useSe); obj.Add("sysUser", sysUser); obj.Add("conectIp", conectIp); obj.Add("dataUsgqty", dataUsgqty); jarr.Add(obj); } if(rdr != null) rdr.Close(); for (int i = 0; i < arrList.Count; i++) { string url = "https://log.smart-factory.kr/apisvc/sendLogDataJSON.do"; string message = "logData=" + jarr[i].ToString(); string code = ""; string rslt = ""; Post(url, message, out code,out rslt); if (code.Equals("AP1002")) { cmd.CommandText = "UPDATE T_SYS_LOG_SF SET SEND_YN= 'Y' WHERE IDX=" + arrList[i]; cmd.ExecuteNonQuery(); Console.Write("전송 성공!!!"); } else { Console.WriteLine("전송 실패!!! ===> 실패코드:"+ code+",\t"+"실패사유:"+rslt); break; } } } catch (Exception ex) { Console.WriteLine(ex.Message); //throw new Exception(ex.Message); } finally { if (conn != null) { conn.Close(); } } } } }