简体   繁体   中英

Calling a datetime function from Web Service C# .net

I have a GetLocalTime() method in my Web Service which I want to call in my form.cs and set the time on web server on the device on button click? So what should I write on the button click event to call the datetime function and set the time?

Method in Web Service:

[WebMethod]
        public DateTime GetLocalTime()
        {
            OracleBridge ob = new OracleBridge(_connStr);
            string sqlQuery = "select sysdate from dual";

            DateTime dt = new DateTime();
            try
            {

                dt = Convert.ToDateTime(ob.ExecuteScalar(sqlQuery));

            }
            catch (OracleException ex)
            {
                throw ex;
            }

            return dt;
        }

Button click event in form.cs to get and set the time:

 private void btnSync_Click(object sender, EventArgs e)
        {

                           ????????????               // what should I write here to call the method
                DateTime localDateTime = ?????????;  //what should I write here pass datetime to setsystemdatetime


                SetSystemDateTime(localDateTime);

                System.Threading.Thread.Sleep(500);
                SetSystemDateTime(localDateTime); 
}

The .NET CLR does not expose any method for setting the time. If you are hell bent on doing this, you will need to work with the Win32 SetLocalTime API and import it .

I advise against allowing a web server process to set the system clock. When you are able to set the clock, you can cause all sorts of security mechanisms to be bypassed; for example, a malicious user can cause sessions or certificates that are otherwise expired to appear valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM