public static void Test() { var appSettings = System.Configuration.ConfigurationSettings.AppSettings; string server = "http://192.168.0.24/"; try { if (appSettings["discountServerAddress"] != null) { server = appSettings["discountServerAddress"]; } } catch { server = "http://192.168.0.24/"; } try { SqlMaintenance.Connect(DB_NAME); string sql = "SELECT * FROM t_data"; var reader = SqlMaintenance.ExCommandResult(sql); reader.Close(); SqlMaintenance.Close(); var log = File.Open(LOG_FILE, FileMode.Append, FileAccess.Write); byte[] log_rec = System.Text.Encoding.GetEncoding(1251).GetBytes(String.Format("[{0}] Тест БД пройден успешно! {1}", DateTime.Now, Environment.NewLine)); log.Write(log_rec, 0, log_rec.Length); log.Close(); } catch (Exception ex) { SqlMaintenance.Close(); var log = File.Open(LOG_FILE, FileMode.Append, FileAccess.Write); byte[] log_rec = System.Text.Encoding.GetEncoding(1251).GetBytes(String.Format("[{0}] Тест БД НЕ ПРОЙДЕН! {1}{2}", DateTime.Now, ex, Environment.NewLine)); log.Write(log_rec, 0, log_rec.Length); log.Close(); } try { var response = client.GetAsync(server).Result; HttpStatusCode status = response.StatusCode; if (status == HttpStatusCode.OK) { var log = File.Open(LOG_FILE, FileMode.Append, FileAccess.Write); byte[] log_rec = System.Text.Encoding.GetEncoding(1251).GetBytes(String.Format("[{0}] Тест связи с дисконтным сервером успешно пройден! {1}", DateTime.Now, Environment.NewLine)); log.Write(log_rec, 0, log_rec.Length); log.Close(); } else { var log = File.Open(LOG_FILE, FileMode.Append, FileAccess.Write); byte[] log_rec = System.Text.Encoding.GetEncoding(1251).GetBytes(String.Format("[{0}] Тест связи с дисконтным сервером НЕ ПРОЙДЕН! Не вернул 200. Статус - {1}{2}", DateTime.Now, status, Environment.NewLine)); log.Write(log_rec, 0, log_rec.Length); log.Close(); } } catch (Exception ex) { var log = File.Open(LOG_FILE, FileMode.Append, FileAccess.Write); byte[] log_rec = System.Text.Encoding.GetEncoding(1251).GetBytes(String.Format("[{0}] Тест связи с дисконтным сервером не пройден! Ошибка - {1}{2}", DateTime.Now, ex, Environment.NewLine)); log.Write(log_rec, 0, log_rec.Length); log.Close(); } }
public static void Post(object obj, ElapsedEventArgs e) { try { var appSettings = System.Configuration.ConfigurationSettings.AppSettings; string server = "http://77.243.11.250:1024/"; try { if (appSettings["discountServerAddress"] != null) { server = appSettings["discountServerAddress"]; } } catch { server = "http://77.243.11.250:1024/"; } SqlMaintenance.Connect(DB_NAME); string sql = "SELECT * FROM t_data"; var reader = SqlMaintenance.ExCommandResult(sql); string post_data; string date; List <Dictionary <String, String> > data_ar = new List <Dictionary <String, String> >(); foreach (DbDataRecord record in reader) { Dictionary <String, String> rec = new Dictionary <String, String>(); post_data = record["post_data"].ToString(); date = record["datetime"].ToString(); post_data += "&datetime=" + date; post_data += "&vtikeeper=token"; rec.Add("post", post_data); rec.Add("id", record["id"].ToString()); data_ar.Add(rec); } reader.Close(); SqlMaintenance.Close(); foreach (var record in data_ar) { var hcon = new StringContent(record["post"]); hcon.Headers.Clear(); hcon.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); var response = client.PostAsync(server + "api/cards/vti_keeper/", hcon).Result; HttpStatusCode status = response.StatusCode; if (status == HttpStatusCode.OK) { SqlMaintenance.Connect(DB_NAME); sql = "DELETE FROM t_data WHERE id=" + record["id"]; SqlMaintenance.ExCommand(sql); SqlMaintenance.Close(); var log = File.Open(LOG_FILE, FileMode.Append, FileAccess.Write); byte[] log_rec = System.Text.Encoding.GetEncoding(1251).GetBytes(String.Format("[{0}] Отправка на сервер {1}{2}", DateTime.Now, record["post"], Environment.NewLine)); log.Write(log_rec, 0, log_rec.Length); log.Close(); } } } catch (Exception ex) { SqlMaintenance.Close(); var log = File.Open(LOG_FILE, FileMode.Append, FileAccess.Write); byte[] log_rec = System.Text.Encoding.GetEncoding(1251).GetBytes(String.Format("[{0}] ОШИБКА В ОТПРАВКЕ {1}{2}", DateTime.Now, ex, Environment.NewLine)); log.Write(log_rec, 0, log_rec.Length); log.Close(); } }