调用webservice校时

先调用网络获取网络时间

namespace Utility
{
    /// <summary>
    /// 网络时间
    /// </summary>
    public class NetTime
    {
        /// <summary>
        /// 获取标准北京时间,读取http://www.beijing-time.org/time.asp
        /// </summary>
        /// <returns>返回网络时间</returns>
        public DateTime GetBeijingTime()
        {
            DateTime dt;
            WebRequest wrt = null;
            WebResponse wrp = null;
            try
            {

                wrt = WebRequest.Create("http://api.k780.com:88/?app=life.time&appkey=22839&sign=2e68c9cb4b8f921ed4031057e0eff33d&format=xml");
                wrp = wrt.GetResponse();
                string html = string.Empty;
                using (Stream stream = wrp.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
                    {
                        html = sr.ReadToEnd();
                    }
                }
                int head = html.IndexOf("datetime_1") + 11;
                int end = html.LastIndexOf("datetime_1") - 2;
                html = html.Substring(head, end - head);

                dt = DateTime.Parse(html);
            }

            catch (WebException)
            {
                return DateTime.Now;//DateTime.Parse("2011-1-1");
            }
            catch (Exception)
            {
                return DateTime.Now;//DateTime.Parse("2011-1-1");
            }
            finally
            {
                if (wrp != null)
                    wrp.Close();
                if (wrt != null)
                    wrt.Abort();
            }
            return dt;
        }
    }
}

在调用系统API设置系统时间更新

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Runtime;

namespace Utility
{
/// <summary>
/// 更新系统时间
/// </summary>
public class UpdateTime
{
    //设置系统时间的API函数
    [DllImport("kernel32.dll")]
    private static extern bool SetLocalTime(ref SYSTEMTIME time);
    [StructLayout(LayoutKind.Sequential)]
    private struct SYSTEMTIME
    {
        public short year;
        public short month;
        public short dayOfWeek;
        public short day;
        public short hour;
        public short minute;
        public short second;
        public short milliseconds;
    }
    /// <summary>
    /// 设置系统时间
    /// </summary>
    /// <param name="dt">需要设置的时间</param>
    /// <returns>返回系统时间设置状态,true为成功,false为失败</returns>
    public static bool SetDate(DateTime dt)
    {
        SYSTEMTIME st;
        st.year = (short)dt.Year;
        st.month = (short)dt.Month;
        st.dayOfWeek = (short)dt.DayOfWeek;
        st.day = (short)dt.Day;
        st.hour = (short)dt.Hour;
        st.minute = (short)dt.Minute;
        st.second = (short)dt.Second;
        st.milliseconds = (short)dt.Millisecond;
        bool rt = SetLocalTime(ref st);
        return rt;
    }
}

}

这样就可以针对时间问题进行网络校时。

原文地址:https://www.cnblogs.com/kingkie/p/7015964.html