2013-03-23 109 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