1 using Newtonsoft.Json; 2 using System; 3 using System.Collections.Generic; 4 using System.Configuration; 5 using System.Dynamic; 6 using System.Linq; 7 using System.Net; 8 using System.Net.Http; 9 10 namespace VerifyServerTime 11 { 12 class Program 13 { 14 private static readonly string DEFAULT_Net = "http://www.baidu.com"; 15 static void Main(string[] args) 16 { 17 Run(); 18 } 19 private static void Run() 20 { 21 var client = new HttpClient(); 22 var result = client.GetAsync(DEFAULT_Net).Result; 23 var BeijingTime = result.Headers.Date.Value.LocalDateTime; 24 25 var SystemTime = DateTime.Now; 26 DateTime dateTime1 = Convert.ToDateTime(BeijingTime); 27 DateTime dateTime2 = Convert.ToDateTime(SystemTime); 28 TimeSpan ts1 = new TimeSpan(dateTime1.Ticks); 29 TimeSpan ts2 = new TimeSpan(dateTime2.Ticks); 30 TimeSpan ts = ts1.Subtract(ts2).Duration(); 31 var dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒"; 32 33 Console.WriteLine("北京时间:" + BeijingTime); 34 Console.WriteLine("服务器时间:" + SystemTime); 35 36 var DiffSeconds = int.Parse(ConfigurationManager.AppSettings["DiffSeconds"]); 37 var EnterpriseWechatApi = ConfigurationManager.AppSettings["EnterpriseWechatApi"]; 38 39 if ((Math.Abs(ts.Seconds) > DiffSeconds) || ts.Minutes>0) 40 { 41 //获取iPv4地址 42 IPAddress iip = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault(a => a.AddressFamily.ToString().Equals("InterNetwork")); 43 44 var client2 = new HttpClient(); 45 string msg = string.Format(@"【{0}】服务器时间:{1},北京时间:{2},时间差异:{3}", iip, SystemTime, BeijingTime , dateDiff); 46 string json = MessageContent(msg); 47 HttpContent content = new StringContent(json); 48 content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); 49 var result2 = client2.PostAsync(EnterpriseWechatApi, content).Result; 50 Console.WriteLine("时间差异:" + json); 51 } 52 //var c = Console.ReadLine(); 53 //if (c == "1") 54 //{ 55 // Run(); 56 //} 57 } 58 59 private static string MessageContent(string msg) 60 { 61 List<string> mentioned_list = new List<string>(); 62 mentioned_list.Add("@sol"); 63 mentioned_list.Add("@all"); 64 65 Dictionary<string, object> matchKV = new Dictionary<string, object>(); 66 matchKV.Add("content", msg); 67 matchKV.Add("mentioned_list", mentioned_list); 68 dynamic obj = new ExpandoObject(); 69 obj.msgtype = "text"; 70 obj.text = matchKV; 71 72 string json = JsonConvert.SerializeObject(obj); 73 return json; 74 } 75 } 76 77 78 }