In doing YH maintenance, occasionally encountered such a problem: the computer's abnormal shutdown caused system time error (changed to 2002-1-1), thus affecting the use of the project system. In particular, the system designed to money, if time is wrong, may lead to unimaginable consequences. So we may need to use the system and the network dual authentication.
By collecting, modifying, optimizing and testing, some errors and slow speeds are eliminated, leaving only 4 viable solutions. There are 3 main categories of these programmes:
Get the server to respond to requests by sending a request to a Web site
Second, get a time page of HTML or XML code, read the time.
Third, by sending a request to a service server to obtain network time
I've encapsulated these methods in a class library.
The following are the specific classes: the NetTime Class (which includes a variety of ways to get the network time, marked with speed), and needs to add references: 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 {///<summary>///network Time///</summary> public class NetTime {#
Region get standard 1 speed 100ms///<summary>///[1]. Get standard 1, read http://www.beijing-time.org/time.asp
</summary>///<returns></returns> public DateTime getbeijingtime () { #region Format//T0=new Date (). GetTime (); nyear=2012; nmonth=2;
nday=11; nwday=6; nhrs=0; nmin=23;
nsec=0;
#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];
for (int i = 0; i < temparray.length; i++) {//Temparray[i] = Temparray[i].
Replace ("\ r \ n", ""); }//string year = temparray[1]. Substring (Temparray[1].
IndexOf ("nyear=") + 6); String month = Temparray[2]. Substring (Temparray[2].
IndexOf ("nmonth=") + 7); String day = Temparray[3]. Substring (Temparray[3].
IndexOf ("nday=") + 5); String hour = Temparray[5]. Substring (Temparray[5].
IndexOf ("nhrs=") + 5); String minite = Temparray[6]. Substring (Temparray[6].
IndexOf ("nmin=") + 5); string second = Temparray[7]. Substring (Temparray[7].
IndexOf ("nsec=") + 5);
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 Get web site response request time, speed 200ms///<summary>///[2] Get web site response request time, speed 200ms </summary>///<param name= "HURL" > Web site </param>///<returns>DateTime<
/returns>///<remarks></remarks> public DateTime getnettime (string hUrl) { string datetxt = null; Request Response Time string[] date1 = null; Split week and date string date2 = ""; The split date and GMT string[] date3 = null;
The final forming date datetime ntime =datetime.today;
String localtime = "";
String mon = "";
XMLHTTP objhttp = new XMLHTTP (); Objhttp.open ("Get", HUrl, FALSE);
try {objhttp.send (); Gets the date and time of the Web site response request.
such as: Wed, Feb 06:34:58 GMT datetxt = Objhttp.getresponseheader ("Date"); Split Time Date1 = Datetxt.
Split (', ');
catch (Exception ex) {throw ex;
}//if (Date1 ==null) {localtime = "network authentication failed, please restart or check network settings"; else if (date1.
Length < 1 {localtime = "network authentication failed, please reboot or check network settings"; else {//remove GMT in time Date2 = date1[1].
Replace ("GMT", ""); such as: Feb 06:34:58 GMT date3 = date2.
Split (")";
such as: Feb 06:34:58 switch (date3[2]) {case "": Mon = "01";
Break
Case "Feb": Mon = "02";
Break
Case "Mar": Mon = "03";
Break
Case "APR": Mon = "04";
Break
Case "May": Mon = "05";
Break
Case "June": Mon = "06";
Break
Case "June": 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
///Final feedback is date and time localtime = date3[3] + "/" + Mon + "/" + date3[1] + "" + date3[4];
Acquired coordinated world time DateTime stime = Convert.todatetime (localtime);
The time to convert to the current computer's timezone, that is, East eight time ntime = TimeZone.CurrentTimeZone.ToLocalTime (stime);
} objhttp = null;
return ntime; #endregion #region Get standard 3, speed 500ms-1500ms///<summary>///[3] Get standard 2, read (x
ML) http://www.time.ac.cn/timeflash.asp?user=flash///</summary>///<returns></returns> Public DateTime Getstandardtime () {#region file format/////<?xml version= "1.0" en coding= "GB2312"?>//-<ntsc>//-<time>//<year>2011</year>/<month>7</month> <day>10</day>//<weekday/>//
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/tools/