2013-03-23 106 views
0

我有一个网络上的Windows应用程序可以在该网络中的多个位置执行,但问题是,它依赖于日期和时间。如何获取网络系统日期时间使用该系统的IP

有没有可能从服务器使用IP或任何其他方式获取DateTime? 所以我可以保持相同的日期和时间。

+0

这通常是通过使NTP服务器一体机完成。所有其他机器定期更新其日期/时间。而NTP服务器当然会从上游NTP服务器更新其日期/时间。 – 2013-03-23 14:20:23

回答

0

尝试实施下列>>

public static DateTime GetNistTime() 
{ 
    DateTime dt = DateTime.MinValue; 

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://...."); 
    req.Method = "GET"; 
    req.Accept = "text/html, application/xhtml+xml, */*"; 
    req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; 
    req.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); //No caching 
    HttpWebResponse res = (HttpWebResponse)req.GetResponse(); 
    if (res.StatusCode == HttpStatusCode.OK) 
    { 
     StreamReader st = new StreamReader(res.GetResponseStream()); 
     string html = st.ReadToEnd().ToUpper(); 
     string time = Regex.Match(html, @">\d+:\d+:\d+<").Value; //HH:mm:ss format 
     string date = Regex.Match(html, @">\w+,\s\w+\s\d+,\s\d+<").Value; //dddd, MMMM dd, yyyy 
     dt= DateTime.Parse((date + " " + time).Replace(">", "").Replace("<", "")); 
    } 

    return dt; 
} 
+0

不知道一个简单的HTTP Get请求如何获得服务器时间?我相信你将不得不实施NTP(网络时间协议)来获取服务器时间。 – 2013-03-23 07:41:25