zoukankan      html  css  js  c++  java
  • c#获取网络时间

            public static DateTime GetInternetDate()
            {
                var client = new TcpClient("time.nist.gov", 13);
                using (var streamReader = new StreamReader(client.GetStream()))
                {
                    var response = streamReader.ReadToEnd();
                    var utcDateTimeString = response.Substring(7, 17);
                    var localDateTime = DateTime.ParseExact(utcDateTimeString, "yy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
                    return localDateTime;
                }
            }

            public static DateTime GetInternetDate1()
            {
                //var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
                var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.baidu.com/");
                var response = myHttpWebRequest.GetResponse();
                string todaysDates = response.Headers["date"];
                DateTime dt = DateTime.ParseExact(todaysDates,
                                           "ddd, dd MMM yyyy HH:mm:ss 'GMT'",
                                           CultureInfo.InvariantCulture.DateTimeFormat,
                                           DateTimeStyles.AssumeUniversal);
                return dt;
            }

  • 相关阅读:
    NanUI for Winform发布,让Winform界面设计拥有无限可能
    使用Nginx反向代理 让IIS和Tomcat等多个站点一起飞
    jQuery功能强大的图片查看器插件
    Entity Framework 5.0 Code First全面学习
    封装EF code first用存储过程的分页方法
    NET中使用Redis (二)
    Redis学习笔记~Redis主从服务器,读写分离
    ID3算法
    定性归纳(1)
    js加密
  • 原文地址:https://www.cnblogs.com/tianmochou/p/6605072.html
Copyright © 2011-2022 走看看