【问题标题】:how can get DateTime From Internet (External Resource - Not From Server)如何从 Internet 获取 DateTime(外部资源 - 不是来自服务器)
【发布时间】:2011-07-13 08:34:00
【问题描述】:

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

提前致谢

【问题讨论】:

  • 您是要从时间服务器还是从客户端机器获取时间?
  • 亲爱的@Alex R. 当然时间服务器更适合我的目的!

标签: c# asp.net web-services


【解决方案1】:

我认为这会帮助你摆脱它。应用下面提到的代码从互联网上检索日期。

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;
    }

【讨论】:

  • @dear palaniappan ...感谢您的精彩回答!我想我得到了答案(我正在测试它)!但是为了完成你的答案,有没有使用网络服务的服务器?
  • 您好,我们也可以在不使用网络服务的情况下使用它
  • 以上代码将返回什么...如果有任何日期时间,那么它将返回哪个国家/地区的日期和时间?
  • 您应该避免使用 IP o DNS,因为其中一些不再可用。阅读 NIST Internet 时间服务器 (NIST link) 的所有说明。重要提示:全局地址 time.nist.gov 以循环顺序解析为以下所有服务器地址,以均衡所有服务器的负载。无论您使用名称还是 IP 地址连接到服务器,将特定服务器名称或地址“硬编码”到设备中以使最终用户无法更改这些参数都是一种不好的做法。
【解决方案2】:

你不能,不是通过使用DateTime.Now。它是DateTime 结构的属性,不能被覆盖。

您可以使用NTP server 来获取时间(尽管 BCL 中不支持)。

【讨论】:

  • 亲爱的@Oded 感谢您的回答!你能解释更多吗!我如何在 vs2010 中实现它并将它与 asp.net 和 c# 一起使用?
【解决方案3】:

为什么不使用 HttpRequest 来查询a site 的 RSS 提要,它给出了世界时间?这是你的意思吗?

【讨论】:

  • 亲爱的@Extrakun:通过这种方式,我如何通过 c# 代码获取当前日期时间 - 我不想向我的用户显示/只想通过 c# 代码获取当前时间并使用它用于被动锁定页面!
【解决方案4】:

你为什么要从互联网上获取它,你仍然会从别人的服务器上获取它。

【讨论】:

  • 亲爱的@JohnCoops786:出于一些关于我的网络托管服务器的安全原因,我想从我的服务器中读取日期时间!
【解决方案5】:

您可以使用 JavaScript 获取日期时间

    myDate = new Date();
    myday = myDate.getDay();
    mymonth = myDate.getMonth();
    myweekday= myDate.getDate();

查看这篇文章到javascript date and time object

看看这个JavaScript Date Object - 死链接修复

【讨论】:

  • 这将获得 client 日期时间。 任何个客户端,所以不同的客户端不会互相同意。
  • 我想他在找客户DateTime
  • 我认为这很不清楚,他没有说明日期应该来自哪里(互联网???)。
  • 感谢 cmets / 但有时客户端日期时间不正确,因此不能成为获取日期时间的好目标 ...
  • 问题很清楚 - 如何从 Internet 获取 DateTime,那么为什么使用 JavaScript 给出愚蠢的答案。此外,在 cmets 中,他表示他认为用户正在寻找客户端 DateTime。这就是为什么投反对票
猜你喜欢
  • 2017-07-18
  • 2015-04-18
  • 2021-08-10
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多