c# – 如何从Internet获取DateTime(外部资源 – 不是来自服务器)

如何从Internet获取当前时间(外部资源 – 不是来自服务器)?
编辑
例如来自以下网站:
http://www.timeanddate.com/worldclock
原因
我会在一个月后将我的页面重定向一个锁定页面(通过检查DateTime.Now),在该页面中,用户应该输入激活码以便返回…
出于某些安全原因,我想从我的服务器中获取当前日期/时间…

提前致谢

解决方法

我认为这将帮助你摆脱它.应用下面提到的代码从Internet检索日期.
public static DateTime GetFastestNISTDate()
{
    var result = DateTime.MinValue;

    // Initialize the list of NIST time servers
    // http://tf.nist.gov/tf-cgi/servers.cgi
    string[] servers = new string[] {
        "nist1-ny.ustiming.org","nist1-nj.ustiming.org","nist1-pa.ustiming.org","time-a.nist.gov","time-b.nist.gov","nist1.aol-va.symmetricom.com","nist1.columbiacountyga.gov","nist1-chi.ustiming.org","nist.expertsmi.com","nist.netservicesgroup.com"
};

        // Try 5 servers in random order to spread the load
        Random rnd = new Random();
        foreach (string server in servers.OrderBy(s => rnd.NextDouble()).Take(5))
        {
            try
            {
                // Connect to the server (at port 13) and get the response
                string serverResponse = string.Empty;
                using (var reader = new StreamReader(new System.Net.sockets.TcpClient(server,13).GetStream()))
                {
                    serverResponse = reader.ReadToEnd();
                }

                // If a response was received
                if (!string.IsNullOrEmpty(serverResponse))
                {
                    // Split the response string ("55596 11-02-14 13:54:11 00 0 0 478.1 UTC(NIST) *")
                    string[] tokens = serverResponse.Split(' ');

                    // Check the number of tokens
                    if (tokens.Length >= 6)
                    {
                        // Check the health status
                        string health = tokens[5];
                        if (health == "0")
                        {
                            // Get date and time parts from the server response
                            string[] dateParts = tokens[1].Split('-');
                            string[] timeParts = tokens[2].Split(':');

                            // Create a DateTime instance
                            DateTime utcDateTime = new DateTime(
                                Convert.ToInt32(dateParts[0]) + 2000,Convert.ToInt32(dateParts[1]),Convert.ToInt32(dateParts[2]),Convert.ToInt32(timeParts[0]),Convert.ToInt32(timeParts[1]),Convert.ToInt32(timeParts[2]));

                            // Convert received (UTC) DateTime value to the local timezone
                            result = utcDateTime.ToLocalTime();

                            return result;
                            // Response successfully received; exit the loop

                        }
                    }

                }

            }
            catch
            {
                // Ignore exception and try the next server
            }
        }
        return result;
    }

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...