怎样才能从日期时间Internet(外部资源 - 无法从服务器) [英] how can get DateTime From Internet (External Resource - Not From Server)

查看:129
本文介绍了怎样才能从日期时间Internet(外部资源 - 无法从服务器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能得到当前的时间从互联网(外部资源 - 无法从服务器)?结果
编辑结果
例如,从以下网址:结果
http://www.timeanddate.com/worldclock 结果
原因结果
我将我的重定向页面,一个页面锁定,一个月后(通过检查DateTime.Now),并在该页面的用户需要输入激活code正在添加为回来...结果
对于一些安全方面的原因,我想获得当前的日期/时间上传出我的服务器...

how can i get current time From Internet (External Resource - Not From Server)?
Edited
For Example From the Below WebSite :
http://www.timeanddate.com/worldclock
Reason
i will redirect my pages to a Lock page after one month (by checking DateTime.Now) and in that page user should input activation code for comming back...
for some security reasons i want to get the current Date/Time From Out Of My Server...

在此先感谢

推荐答案

我认为这将帮助你下面提到的code的it.Apply从互联网获取的日期。

I think this will help you out of it.Apply the below mentioned code for retrieving the date from 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;
    }

这篇关于怎样才能从日期时间Internet(外部资源 - 无法从服务器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆