static void Main(string[] args) { //LogWritter log = LogWritter.GetInstance(); try { Config config = new Config(); if (CheckForInternetConnection()) { SenderDataToSF sender = new SenderDataToSF(config); while (true) { if (CheckForInternetConnection()) { try { CSVParser result = new CSVParser(config.item.fileDir, null); LogWritter.GetInstance().WriteLog("Objects to send : " + result.itemsList.Count()); if (result.itemsList.Count() > 0) { sender.SendData(result.itemsList); LogWritter.GetInstance().WriteLog("Data send successfull"); } } catch (NullReferenceException e) { LogWritter.GetInstance().WriteLog("Error : " + e.Message + "\n Stack trace: " + e.ToString()); LogWritter.GetInstance().WriteLog(" Application closed "); Thread.Sleep(60000); } } Thread.Sleep(config.item.timeout * 1000); } } else { LogWritter.GetInstance().WriteLog(" Connection is interapted "); LogWritter.GetInstance().WriteLog(" Application closed "); Thread.Sleep(60000); var fileName = Assembly.GetExecutingAssembly().Location; System.Diagnostics.Process.Start(fileName); Environment.Exit(0); } } catch (Exception e) { LogWritter.GetInstance().WriteLog("Error : " + e.Message + "\n Stack trace: " + e.ToString()); LogWritter.GetInstance().WriteLog(" Application closed "); Thread.Sleep(60000); var fileName = Assembly.GetExecutingAssembly().Location; System.Diagnostics.Process.Start(fileName); Environment.Exit(0); } }
public void SendData(List <RowItem> itemList) { List <ResponseWrapper> result; if (this.token.access_token != null) { try { String jsonBody = JsonConvert.SerializeObject(itemList); WebRequest request = (HttpWebRequest)WebRequest.Create(this.config.item.endpoint); request.Method = "POST"; byte[] byteArray = Encoding.UTF8.GetBytes(jsonBody); request.ContentType = "application/json"; request.Headers.Add("Authorization", "Bearer " + this.token.access_token); request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); //WebResponse response = request.GetResponse(); result = JsonConvert.DeserializeObject <List <ResponseWrapper> >(new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd()); this.saveLastItem(itemList[itemList.Count - 1]); } catch (WebException ex) { result = JsonConvert.DeserializeObject <List <ResponseWrapper> >(new StreamReader(ex.Response.GetResponseStream()).ReadToEnd()); } ResponseWrapper responseFromSF = result[0]; if (responseFromSF.message.Equals("Session expired or invalid")) { this.CreateToken(); this.SendData(itemList); } else { if (!responseFromSF.message.Equals("success")) { LogWritter.GetInstance().WriteLog("Error from send request method: " + responseFromSF); } else { LogWritter.GetInstance().WriteLog("Response : " + responseFromSF); } } } else { throw new System.Security.SecurityException(" Access token not created "); } }