用AJAX来远程获取www.time.ac.cn/timeflash.asp?user=flash的内容,然后可以用正则来获取你想要的内容,或是直接处理XML文件

解决方案 »

  1.   

    在网络..不可能传递精确的时间的...因为网络延迟的原因..
    因为网延不是一个恒定值...不象光速是一个恒定值..
    假设你有一个你需要的时间源,你希望将这个时间源在全球发布..我的做法是..
    利用需要获得时间源的本地计算机的时钟辅助处理,因为这个计算机的时钟相对网延还是相当精确的..
    要作的步骤如下..
    1 向Server发送时间请求,将发送的本地时间记录为T1
    2 Server 返回Server 时间 T2,同时记录本地时间为T3
    3 计算时间..
    具体作法是 T3-T1=T4 ,T4就是是网延,假设发送网延和接收网延是相同的..
    那么就推导出..
    当客户端的时间为T1时 Server的时间为 T2-(T4/2)
    这样就可以计算出客户端和Server的时差了...
    当获取Server的时间时利用本地时钟经过计算就可以得出相对精确的时间了..
    当然以上步骤可以每隔一段时间进行一次...
    为了更精确,还可以在一次的重复执行步骤1,2,3获得平均值..来得到更精确的时间差..
      

  2.   

    zhiang75,如果不介意的话,希望你能给我一段代码以说明.假设,以:
    http://www.time.ac.cn/timeflash.asp?user=flash 
    这个链接为服务器的基准时间.
    谢谢!
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    using System.Xml;namespace WindowsApplication7
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        TimeSpan timeSpan = new TimeSpan();        private void button1_Click(object sender, EventArgs e)
            {
                // Create a request for the URL. 
                WebRequest request = WebRequest.Create("http://www.time.ac.cn/timeflash.asp?user=flash");
                // If required by the server, set the credentials.
                request.Credentials = CredentialCache.DefaultCredentials;            DateTime T1 = DateTime.Now;
                // Get the response.
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                DateTime T2 = DateTime.Now;
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();            XmlDocument doc = new XmlDocument();
                doc.Load(dataStream);
                dataStream.Close();
                int y = 0;
                int m = 0;
                int d = 0;
                int H = 0;
                int M = 0;
                int S = 0;            XmlNode time = doc.SelectSingleNode("descendant::time");
                XmlNode temp = time.SelectSingleNode("year");
                y = XmlConvert.ToInt32(temp.InnerText);
                temp = time.SelectSingleNode("month");
                m = XmlConvert.ToInt32(temp.InnerText);
                temp = time.SelectSingleNode("day");
                d = XmlConvert.ToInt32(temp.InnerText);
                temp = time.SelectSingleNode("hour");
                H = XmlConvert.ToInt32(temp.InnerText);
                temp = time.SelectSingleNode("minite");
                M = XmlConvert.ToInt32(temp.InnerText);
                temp = time.SelectSingleNode("second");
                S = XmlConvert.ToInt32(temp.InnerText);
                DateTime T3 = new DateTime(y, m, d, H, M, S);
                TimeSpan ts = T2 - T1;
                long bl1 = ts.Ticks / 2;
                T3 = T3.AddTicks(-bl1);
                timeSpan = T3 - T1;
            }        int bi1 = 0;
            private void timer1_Tick(object sender, EventArgs e)
            {
                bi1++;
                if (bi1 == 60)
                {
                    bi1 = 0;
                    button1_Click(sender, e);
                }
                this.Text = DateTime.Now.Add(timeSpan).ToLongTimeString();
            }
        }
    }namespace WindowsApplication7
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                this.button1 = new System.Windows.Forms.Button();
                this.timer1 = new System.Windows.Forms.Timer(this.components);
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(217, 121);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(102, 35);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // timer1
                // 
                this.timer1.Enabled = true;
                this.timer1.Interval = 1000;
                this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(467, 264);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Timer timer1;
        }
    }
      

  4.   

    zhiang75,sorry!我是想把时间用在HTML网页中
      

  5.   

    查看javascript window.XMLHttpRequest
    再具体,就基本把代码给你了另外7楼也说得比较多,你可以2分钟对准时间一次就可以了