标签:
转自:http://blog.csdn.net/xiaoxian8023/article/details/7250385
在做YH维护的时候,偶尔会碰到这样的问题:电脑的非正常关机导致系统时间出错(变为了2002-1-1),从而影响到项目系统的使用。尤其是设计到money的系统,如果时间错误,可能会导致无法想象的后果。所以我们可能需要用系统和网络的双重验证。
通过收集、修改、优化和测试,剔除了一些错误的和速度超慢的,只剩下了4种可行的方案。这些方案中主要有3类:
一、通过向某网站发送请求,获取服务器响应请求的时间
二、获某时间网页的html或xml码,读取其中的时间。
三、通过向某授时服务器发送请求,获取网络时间
我把这些方法封装到了一个类库里了。
下面是具体的类:NetTime类(包含了多种获取网络时间的方法,标有速度),需要添加引用:COM-Microsoft Xml 3.0
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using MSXML2;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Text.RegularExpressions;
- using System.IO.Compression;
- using System.Xml;
-
-
- namespace WebTime
- {
-
-
-
- public class NetTime
- {
- #region 获取标准北京时间1 速度100ms
-
-
-
-
- public DateTime GetBeijingTime()
- {
- #region 格式
-
-
- #endregion
- DateTime dt;
- WebRequest wrt = null;
- WebResponse wrp = null;
- try
- {
- wrt = WebRequest.Create("http://www.beijing-time.org/time.asp");
- wrp = wrt.GetResponse();
-
- string html = string.Empty;
- using (Stream stream = wrp.GetResponseStream())
- {
- using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
- {
- html = sr.ReadToEnd();
- }
- }
-
- string[] tempArray = html.Split(‘;‘);
- for (int i = 0; i < tempArray.Length; i++)
- {
- tempArray[i] = tempArray[i].Replace("\r\n", "");
- }
-
- string year = tempArray[1].Split(‘=‘)[1];
- string month = tempArray[2].Split(‘=‘)[1];
- string day = tempArray[3].Split(‘=‘)[1];
- string hour = tempArray[5].Split(‘=‘)[1];
- string minite = tempArray[6].Split(‘=‘)[1];
- string second = tempArray[7].Split(‘=‘)[1];
-
-
-
-
-
-
-
-
-
-
-
- dt = DateTime.Parse(year + "-" + month + "-" + day + " " + hour + ":" + minite + ":" + second);
- }
- catch (WebException)
- {
- return DateTime.Parse("2011-1-1");
- }
- catch (Exception)
- {
- return DateTime.Parse("2011-1-1");
- }
- finally
- {
- if (wrp != null)
- wrp.Close();
- if (wrt != null)
- wrt.Abort();
- }
- return dt;
- }
- #endregion
-
- #region 获取网站响应请求的时间,速度200ms
-
-
-
-
-
-
- public DateTime GetNetTime( string hUrl)
- {
- string datetxt = null;
- string[] date1 = null;
- string date2 = "";
- string[] date3 = null;
- DateTime nTime =DateTime.Today ;
- string localtime = "";
- string mon = "";
-
- XMLHTTP objHttp = new XMLHTTP();
-
- objHttp.open("GET", hUrl, false);
-
- try
- {
- objHttp.send();
-
-
- datetxt = objHttp.getResponseHeader("Date");
-
-
- date1 = datetxt.Split(‘,‘);
-
- }
- catch (Exception ex)
- {
- throw ex;
- }
-
-
- if (date1 ==null )
- {
- localtime = "网络验证失败,请重新启动或检查网络设置";
- }
- else if (date1.Length < 1)
- {
- localtime = "网络验证失败,请重新启动或检查网络设置";
- }
- else
- {
-
- date2 = date1[1].Replace("GMT", "");
-
-
- date3 = date2.Split(‘ ‘);
-
-
- switch (date3[2])
- {
- case "Jan":
- mon = "01";
- break;
- case "Feb":
- mon = "02";
- break;
- case "Mar":
- mon = "03";
- break;
- case "Apr":
- mon = "04";
- break;
- case "May":
- mon = "05";
- break;
- case "Jun":
- mon = "06";
- break;
- case "Jul":
- mon = "07";
- break;
- case "Aug":
- mon = "08";
- break;
- case "Sep":
- mon = "09";
- break;
- case "Oct":
- mon = "10";
- break;
- case "Nov":
- mon = "11";
- break;
- case "Dec":
- mon = "12";
- break;
- }
-
-
- localtime = date3[3] + "/" + mon + "/" + date3[1] + " " + date3[4];
-
-
- DateTime sTime = Convert.ToDateTime(localtime);
-
-
- nTime = TimeZone.CurrentTimeZone.ToLocalTime(sTime);
- }
- objHttp = null;
- return nTime;
- }
- #endregion
-
- #region 获取标准北京时间3,速度500ms-1500ms
-
-
-
-
- public DateTime GetStandardTime()
- {
- #region 文件格式
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion
-
- DateTime dt;
- WebRequest wrt = null;
- WebResponse wrp = null;
- try
- {
- wrt = WebRequest.Create("http://www.time.ac.cn/timeflash.asp?user=flash");
- wrt.Credentials = CredentialCache.DefaultCredentials;
-
- wrp = wrt.GetResponse();
- StreamReader sr = new StreamReader(wrp.GetResponseStream(), Encoding.UTF8);
- string html = sr.ReadToEnd();
- sr.Close();
- wrp.Close();
-
-
-
-
- html = html.Substring(51, 109);
-
- string[] s1 = html.Split(new char[2] { ‘<‘, ‘>‘ });
- string year = s1[2];
- string month = s1[6];
- string day = s1[10];
- string hour = s1[18];
- string minite = s1[22];
- string second = s1[26];
-
- dt = DateTime.Parse(year + "-" + month + "-" + day + " " + hour + ":" + minite + ":" + second);
- }
- catch (WebException)
- {
- return DateTime.Parse("0001-1-1");
- }
- catch (Exception)
- {
- return DateTime.Parse("0001-1-1");
- }
- finally
- {
- if (wrp != null)
- wrp.Close();
- if (wrt != null)
- wrt.Abort();
- }
- return dt;
- }
- #endregion
-
- #region 访问标准校时服务器端口获取网络时间 速度:1000-2000ms
-
-
-
-
-
-
- public DateTime GetInternetTime(string HostName, int PortNum)
- {
- DateTime official, localtime;
- string returndata = null;
- string[] dates = new string[4];
- string[] times = new string[4];
- string[] tokens = new string[11];
-
- TcpClient tcpclient = new TcpClient();
- try
- {
- tcpclient.Connect(HostName, PortNum);
-
- NetworkStream networkStream = tcpclient.GetStream();
- if (networkStream.CanWrite && networkStream.CanRead)
- {
- Byte[] sendBytes = Encoding.ASCII.GetBytes("Hello");
- networkStream.Write(sendBytes, 0, sendBytes.Length);
- byte[] bytes = new byte[tcpclient.ReceiveBufferSize];
- networkStream.Read(bytes, 0, (int)tcpclient.ReceiveBufferSize);
- returndata = Encoding.ASCII.GetString(bytes);
- }
- tcpclient.Close();
- }
- catch (Exception excep)
- {
- throw excep;
- }
-
- tokens = returndata.Split(‘ ‘);
- dates = tokens[1].Split(‘-‘);
- times = tokens[2].Split(‘:‘);
-
- official = new DateTime(Int32.Parse(dates[0]) + 2000, Int32.Parse(dates[1]), Int32.Parse(dates[2]),
- Int32.Parse(times[0]), Int32.Parse(times[1]), Int32.Parse(times[2]));
- localtime = TimeZone.CurrentTimeZone.ToLocalTime(official);
- return localtime;
-
- }
- #endregion
-
- }
-
- }
先说一下NetTime类,4个方法,
第一个方法GetBeijingTime()速度最快,100ms的反应时间,快得没话说,不过在跟其他校时网比较时间时,它的时间比别的校时网站时间要提前10s。
第二个方法GetNetTime(string Url),反应时间取决于你访问的网页,我这里用的是百度。通过查资料,百度的平均加载速度为:0.48s(2007年),而Google的加载速度为:0.48s(2007年0.87s)。这2个都可以,当然也可以用网易163或者其他的。推荐用这个。我用的Google,反应时间为200ms左右。完全可以满足你的一般需求。
第三个方法GetStandardTime(),网站的加载速度为0.55s。但是处理起来有点费时,反应时间在500-1500ms。
第四个方法GetInternetTime(string HostName,int PortNum),同样取决于授时服务器,我这里用的是time-a.timefreq.bldrdoc.gov,端口号为13,反应时间在1000-2000ms。
网络时间应用举例:
- private void timer2_Tick(object sender, EventArgs e)
- {
-
- try
- {
- NetTime t = new NetTime();
- string time = t.GetBeijingTime().ToString();
- lblNetTime.Text = time;
- }
- catch (Exception ex)
- {
- lblNetTime.Text = ex.Message;
- }
-
- }
-
- private void timer3_Tick(object sender, EventArgs e)
- {
-
- try
- {
- NetTime t = new NetTime();
- string time = t.GetNetTime("http://www.google.com.hk/").ToString();
- label3.Text = time;
- }
- catch (Exception ex)
- {
- label3.Text = ex.Message ;
- }
- }
-
- private void timer4_Tick(object sender, EventArgs e)
- {
-
- try
- {
- NetTime t = new NetTime();
- string time = t.GetStandardTime().ToString();
- label4.Text = time;
- }
- catch (Exception ex)
- {
- label4.Text = ex.Message;
- }
-
- }
-
- private void timer5_Tick(object sender, EventArgs e)
- {
- try
- {
- NetTime t = new NetTime();
- string time = t.GetInternetTime("time-a.timefreq.bldrdoc.gov",13).ToString();
- label5.Text = time;
- }
- catch (Exception ex)
- {
- label5.Text = ex.Message;
- }
- }
前面说系统和网络的双重验证,其实只要判断一下2者 在日期上不同,就用网络时间来纠正一下本地时间即可。
下面是系统时间类
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
-
- namespace WebTime
- {
-
-
-
- public struct SystemTime
- {
- public ushort wyear;
- public ushort wmonth;
- public ushort wdayofweek;
- public ushort wday;
- public ushort whour;
- public ushort wminute;
- public ushort wsecond;
- public ushort wmilliseconds;
-
-
-
-
-
- public void fromDateTime(DateTime time)
- {
- wyear = (ushort)time.Year;
- wmonth = (ushort)time.Month;
- wdayofweek = (ushort)time.DayOfWeek;
- wday = (ushort)time.Day;
- whour = (ushort)time.Hour;
- wminute = (ushort)time.Minute;
- wsecond = (ushort)time.Second;
- wmilliseconds = (ushort)time.Millisecond;
- }
-
-
-
-
-
-
- public DateTime toDateTime()
- {
- return new DateTime(wyear, wmonth, wday, whour, wminute, wsecond, wmilliseconds);
- }
-
-
-
-
-
-
- public static DateTime toDateTime(SystemTime time)
- {
- return time.toDateTime();
- }
- }
-
-
-
-
- public class SysTime
- {
- [DllImport("Kernel32.dll ")]
- public static extern bool SetSystemTime(ref SystemTime SystemTime);
- [DllImport("Kernel32.dll ")]
- public static extern void GetSystemTime(ref SystemTime SystemTime);
- }
- }
其中定义一个系统时间的结构体类型和一个类。SetSystemTime是来设置系统时间,GetSystemTime用来获取系统时间。
下面是用网络时间来校对系统时间的源码:
- private void btnCorretTime_Click(object sender, EventArgs e)
- {
- NetTime nt = new NetTime();
-
-
- string time = nt.GetNetTime("http://www.google.com.hk/").ToString();
-
- DateTime t=Convert.ToDateTime(time);
-
-
-
- SystemTime st = new SystemTime();
-
-
-
- t = TimeZone.CurrentTimeZone.ToUniversalTime(t);
-
- st.fromDateTime(t);
-
-
- SysTime.SetSystemTime(ref st);
- MessageBox.Show("时间改为"+ st.toDateTime().ToString());
- }
所以只需要给项目系统添加一个通过网络时间来校对系统时间功能,而其他代码不用改,还是用系统时间,这样也符合了开闭原则。
转载的朋友请说明出处:http://blog.csdn.net/xiaoxian8023/article/details/7250385
多方法获取网络时间
标签:
原文地址:http://www.cnblogs.com/zhxwaaa/p/4471636.html